Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mock Partially using Dagger

Mock Partially using Dagger

How to mock partially using Dagger 2

3cca191bf3064fd059ea2c3d6022afbd?s=128

Fumihiko Shiroyama

May 09, 2017
Tweet

Transcript

  1. Mock Partially @fushiroyama

  2. Android Testing Bootcamp • https://android-testing- bootcamp.connpass.com/ • https://android-testing- bootcamp.connpass.com/event/55274/

  3. TL;DR • Use Dagger2 • Make Partial Component • Use

    it as Component Dependency • Write Mock Module of that Component • Prepare Test Application class • Specify it in Custom Test Runner
  4. Normal Way

  5. Application Module @Module public class ApplicationModule { @Provides public Info

    providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
  6. Application Module @Module public class ApplicationModule { @Provides public Info

    providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
  7. Application Module @Module public class ApplicationModule { @Provides public Info

    providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
  8. Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent

    plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
  9. Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent

    plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
  10. Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent

    plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
  11. My Application @Override public void onCreate() { super.onCreate(); applicationComponent =

    DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
  12. My Application @Override public void onCreate() { super.onCreate(); applicationComponent =

    DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
  13. My Application @Override public void onCreate() { super.onCreate(); applicationComponent =

    DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
  14. Partial Mock

  15. Application Module @Module public class ApplicationModule { @Provides public Info

    providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
  16. Application Module @Module public class ApplicationModule { // removed }

  17. API Module @Module public class ApiModule { @Provides public Info

    providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
  18. API Module @Module public class ApiModule { @Provides public Info

    providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
  19. API Module @Module public class ApiModule { @Provides public Info

    providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
  20. API Component @Component(modules = ApiModule.class) public interface ApiComponent { Info

    getInfo(); Auth getAuth(); }
  21. API Component @Component(modules = ApiModule.class) public interface ApiComponent { Info

    getInfo(); Auth getAuth(); }
  22. API Component @Component(modules = ApiModule.class) public interface ApiComponent { Info

    getInfo(); Auth getAuth(); }
  23. API Component @Component(modules = ApiModule.class) public interface ApiComponent { Info

    getInfo(); Auth getAuth(); } /FDFTTBSZ
  24. Component Dependency

  25. Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent

    plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
  26. Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent

    plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
  27. Application Component @Component(modules = ApplicationModule.class, dependencies = ApiComponent.clas) public interface

    ApplicationComponent { ActivityComponent plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
  28. Application Component @Component(modules = ApplicationModule.class, dependencies = ApiComponent.clas) public interface

    ApplicationComponent { ActivityComponent plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
  29. Custom Application

  30. My Application @Override public void onCreate() { super.onCreate(); applicationComponent =

    DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
  31. My Application @Override public void onCreate() { super.onCreate(); ApiComponent apiComponent

    = DaggerApiComponent.builder() .apiModule(new ApiModule()) .build(); applicationComponent = DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(apiComponent) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
  32. My Application @Override public void onCreate() { super.onCreate(); ApiComponent apiComponent

    = DaggerApiComponent.builder() .apiModule(new ApiModule()) .build(); applicationComponent = DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(apiComponent) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
  33. My Application @Override public void onCreate() { super.onCreate(); ApiComponent apiComponent

    = DaggerApiComponent.builder() .apiModule(new ApiModule()) .build(); applicationComponent = DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(apiComponent) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
  34. My Application @Override public void onCreate() { super.onCreate(); ApiComponent apiComponent

    = DaggerApiComponent.builder() .apiModule(new ApiModule()) .build(); applicationComponent = DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(apiComponent) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
  35. Mock API Module

  36. Mock API Module @Module public class MockApiModule { private final

    Auth mockAuthApi; private final Info mockInfoApi; public MockApiModule() { mockAuthApi = Mockito.mock(Auth.class); mockInfoApi = Mockito.mock(Info.class); } @Provides Info providesInfo() { return mockInfoApi; } @Provides public Auth providesAuth() { return mockAuthApi; } }
  37. Mock API Module @Module public class MockApiModule { private final

    Auth mockAuthApi; private final Info mockInfoApi; public MockApiModule() { mockAuthApi = Mockito.mock(Auth.class); mockInfoApi = Mockito.mock(Info.class); } @Provides Info providesInfo() { return mockInfoApi; } @Provides public Auth providesAuth() { return mockAuthApi; } }
  38. Mock API Component

  39. Mock API Component @Component(modules = MockApiModule.class) public interface MockApiComponent extends

    ApiComponent { }
  40. Mock API Component @Component(modules = MockApiModule.class) public interface MockApiComponent extends

    ApiComponent { }
  41. Mock API Component @Component(modules = MockApiModule.class) public interface MockApiComponent extends

    ApiComponent { }
  42. Test Application

  43. Test Application public class TestApplication extends MyApplication { @Override public

    ApplicationComponent getApplicationComponent() { MockApiComponent mockApiComponent = DaggerMockApiComponent.builder() .mockApiModule(new MockApiModule()) .build(); return DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(mockApiComponent) .build(); } }
  44. Test Application public class TestApplication extends MyApplication { @Override public

    ApplicationComponent getApplicationComponent() { MockApiComponent mockApiComponent = DaggerMockApiComponent.builder() .mockApiModule(new MockApiModule()) .build(); return DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(mockApiComponent) .build(); } }
  45. Test Application public class TestApplication extends MyApplication { @Override public

    ApplicationComponent getApplicationComponent() { MockApiComponent mockApiComponent = DaggerMockApiComponent.builder() .mockApiModule(new MockApiModule()) .build(); return DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(mockApiComponent) .build(); } }
  46. Test Application public class TestApplication extends MyApplication { @Override public

    ApplicationComponent getApplicationComponent() { MockApiComponent mockApiComponent = DaggerMockApiComponent.builder() .mockApiModule(new MockApiModule()) .build(); return DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(mockApiComponent) .build(); } }
  47. Test Application public class TestApplication extends MyApplication { private MockApiComponent

    mockApiComponent; @Override public ApplicationComponent getApplicationComponent() { mockApiComponent = DaggerMockApiComponent.builder() .mockApiModule(new MockApiModule()) .build(); return DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(mockApiComponent) .build(); } public MockApiComponent getMockApiComponent() { return mockApiComponent; } }
  48. Test Application public class TestApplication extends MyApplication { private MockApiComponent

    mockApiComponent; @Override public ApplicationComponent getApplicationComponent() { mockApiComponent = DaggerMockApiComponent.builder() .mockApiModule(new MockApiModule()) .build(); return DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(mockApiComponent) .build(); } public MockApiComponent getMockApiComponent() { return mockApiComponent; } }
  49. Test Application public class TestApplication extends MyApplication { private MockApiComponent

    mockApiComponent; @Override public ApplicationComponent getApplicationComponent() { mockApiComponent = DaggerMockApiComponent.builder() .mockApiModule(new MockApiModule()) .build(); return DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .apiComponent(mockApiComponent) .build(); } public MockApiComponent getMockApiComponent() { return mockApiComponent; } }
  50. Mock Test Runner

  51. Mock Test Runner public class MockTestRunner extends AndroidJUnitRunner { @Override

    public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { String testApplicationClassName = TestApplication.class.getCanonicalName(); return super.newApplication(cl, testApplicationClassName, context); } }
  52. Mock Test Runner public class MockTestRunner extends AndroidJUnitRunner { @Override

    public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { String testApplicationClassName = TestApplication.class.getCanonicalName(); return super.newApplication(cl, testApplicationClassName, context); } }
  53. Mock Test Runner public class MockTestRunner extends AndroidJUnitRunner { @Override

    public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { String testApplicationClassName = TestApplication.class.getCanonicalName(); return super.newApplication(cl, testApplicationClassName, context); } }
  54. Write Your Test!

  55. Your Android Test @RunWith(AndroidJUnit4.class) public class YourAndroidTest { @Before public

    void setUp() throws Exception { TestApplication application = (TestApplication) InstrumentationRegistry.getTargetContext().getApplicationContext(); MockApiComponent mockApiComponent = application.getMockApiComponent(); Info info = mockApiComponent.getInfo(); when(info.access(any())).thenReturn(result); } @Test public void doYourTest() { } }
  56. Your Android Test @RunWith(AndroidJUnit4.class) public class YourAndroidTest { @Before public

    void setUp() throws Exception { TestApplication application = (TestApplication) InstrumentationRegistry.getTargetContext().getApplicationContext(); MockApiComponent mockApiComponent = application.getMockApiComponent(); Info info = mockApiComponent.getInfo(); when(info.access(any())).thenReturn(result); } @Test public void doYourTest() { } }
  57. Your Android Test @RunWith(AndroidJUnit4.class) public class YourAndroidTest { @Before public

    void setUp() throws Exception { TestApplication application = (TestApplication) InstrumentationRegistry.getTargetContext().getApplicationContext(); MockApiComponent mockApiComponent = application.getMockApiComponent(); Info info = mockApiComponent.getInfo(); when(info.access(any())).thenReturn(result); } @Test public void doYourTest() { } }
  58. Your Android Test @RunWith(AndroidJUnit4.class) public class YourAndroidTest { @Before public

    void setUp() throws Exception { TestApplication application = (TestApplication) InstrumentationRegistry.getTargetContext().getApplicationContext(); MockApiComponent mockApiComponent = application.getMockApiComponent(); Info info = mockApiComponent.getInfo(); when(info.access(any())).thenReturn(result); } @Test public void doYourTest() { } }
  59. Your Android Test @RunWith(AndroidJUnit4.class) public class YourAndroidTest { @Before public

    void setUp() throws Exception { TestApplication application = (TestApplication) InstrumentationRegistry.getTargetContext().getApplicationContext(); MockApiComponent mockApiComponent = application.getMockApiComponent(); Info info = mockApiComponent.getInfo(); when(info.access(any())).thenReturn(result); } @Test public void doYourTest() { } }
  60. Use Your Runner android { defaultConfig { testInstrumentationRunner "your.package.runner.MockTestRunner" }

    }
  61. Thank you!