1 /* 2 * Copyright (C) 2018 The Dagger Authors. 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 17 package dagger.functional.subcomponent; 18 19 import static org.junit.Assert.fail; 20 21 import dagger.Component; 22 import dagger.Module; 23 import dagger.Provides; 24 import dagger.Subcomponent; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Tests for subcomponent factory methods. */ 30 @RunWith(JUnit4.class) 31 public final class SubcomponentFactoryMethodTest { 32 33 @Module 34 static class IntModule { 35 @Provides provideInt()36 int provideInt() { 37 return 42; 38 } 39 } 40 41 @Module 42 static class StringModule { 43 final String s; 44 StringModule(String s)45 StringModule(String s) { 46 this.s = s; 47 } 48 49 @Provides provideString(int i)50 String provideString(int i) { 51 return s + i; 52 } 53 } 54 55 @Component(modules = IntModule.class) 56 interface TestComponent { newSubcomponent(StringModule stringModule)57 TestSubcomponent newSubcomponent(StringModule stringModule); 58 } 59 60 @Subcomponent(modules = StringModule.class) 61 interface TestSubcomponent { string()62 String string(); 63 } 64 65 @Test creatingSubcomponentViaFactoryMethod_failsForNullParameter()66 public void creatingSubcomponentViaFactoryMethod_failsForNullParameter() { 67 TestComponent component = DaggerSubcomponentFactoryMethodTest_TestComponent.create(); 68 try { 69 component.newSubcomponent(null); 70 fail(); 71 } catch (NullPointerException expected) { 72 } 73 } 74 } 75