1 /* 2 * Copyright (C) 2015 Google, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package producerstest.builder; 17 18 import com.google.common.util.concurrent.Futures; 19 import com.google.common.util.concurrent.ListenableFuture; 20 import com.google.common.util.concurrent.MoreExecutors; 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 import org.junit.runners.JUnit4; 24 25 import static com.google.common.truth.Truth.assertThat; 26 27 /** Tests for {@link dagger.producers.ProductionComponent.Builder}. */ 28 @RunWith(JUnit4.class) 29 public final class ProductionComponentBuilderTest { 30 31 @Test 32 public void successfulBuild() throws Exception { 33 TestComponentWithBuilder component = 34 DaggerTestComponentWithBuilder.builder() 35 .executor(MoreExecutors.directExecutor()) 36 .depComponent(depComponent(15.3)) 37 .strModule(new StringModule()) 38 .build(); 39 assertThat(component.s().get()).isEqualTo("arg: 42"); 40 assertThat(component.d().get()).isEqualTo(15.3); 41 } 42 43 @Test 44 public void successfulBuild_withMissingZeroArgModule() throws Exception { 45 TestComponentWithBuilder component = 46 DaggerTestComponentWithBuilder.builder() 47 .executor(MoreExecutors.directExecutor()) 48 .depComponent(depComponent(15.3)) 49 .build(); 50 assertThat(component.s().get()).isEqualTo("arg: 42"); 51 assertThat(component.d().get()).isEqualTo(15.3); 52 } 53 54 @Test(expected = IllegalStateException.class) 55 public void missingExecutor() { 56 DaggerTestComponentWithBuilder.builder() 57 .depComponent(depComponent(15.3)) 58 .strModule(new StringModule()) 59 .build(); 60 } 61 62 @Test(expected = IllegalStateException.class) 63 public void missingDepComponent() { 64 DaggerTestComponentWithBuilder.builder() 65 .executor(MoreExecutors.directExecutor()) 66 .strModule(new StringModule()) 67 .build(); 68 } 69 70 private static DepComponent depComponent(final double value) { 71 return new DepComponent() { 72 @Override 73 public ListenableFuture<Double> d() { 74 return Futures.immediateFuture(value); 75 } 76 }; 77 } 78 } 79