Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Mock Partially using Dagger
Search
Fumihiko Shiroyama
May 09, 2017
Programming
0
650
Mock Partially using Dagger
How to mock partially using Dagger 2
Fumihiko Shiroyama
May 09, 2017
Tweet
Share
More Decks by Fumihiko Shiroyama
See All by Fumihiko Shiroyama
The world of Android wireless communications without Internet
srym
1
130
AWS Device FarmとCircleCIでAndroidのUIテストを自動化しよう
srym
1
5k
Spring BootをKotlinで作成しAmazon Elastic Container Service (ECS) で稼働させる
srym
6
1.9k
iOSDC_2019_DeviceFarm.pdf
srym
8
19k
世界で戦うエンジニアになるために_公開用.pdf
srym
18
45k
Unit Testing in a Nutshell - DroidKaigi 2018
srym
11
15k
Clean Architecture & TDD
srym
1
3.7k
はやい・やすい・うまい!スタートアップでも使える Retrofit + RxJava で瞬間APIクッキングレシピ
srym
2
600
I/O 2017 Short Report
srym
0
300
Other Decks in Programming
See All in Programming
As an Engineers, let's build the CRM system via LINE Official Account 2.0
clonn
1
670
今からはじめるAndroidアプリ開発 2024 / DevFest 2024
star_zero
0
1k
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
300
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
210
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
690
testcontainers のススメ
sgash708
1
120
Criando Commits Incríveis no Git
marcelgsantos
2
170
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Security_for_introducing_eBPF
kentatada
0
110
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
210
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
Beyond ORM
77web
1
160
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
133
9k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Adopting Sorbet at Scale
ufuk
73
9.1k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
How STYLIGHT went responsive
nonsquared
95
5.2k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Side Projects
sachag
452
42k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Transcript
Mock Partially @fushiroyama
Android Testing Bootcamp • https://android-testing- bootcamp.connpass.com/ • https://android-testing- bootcamp.connpass.com/event/55274/
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
Normal Way
Application Module @Module public class ApplicationModule { @Provides public Info
providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
Application Module @Module public class ApplicationModule { @Provides public Info
providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
Application Module @Module public class ApplicationModule { @Provides public Info
providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent
plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent
plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent
plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
My Application @Override public void onCreate() { super.onCreate(); applicationComponent =
DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
My Application @Override public void onCreate() { super.onCreate(); applicationComponent =
DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
My Application @Override public void onCreate() { super.onCreate(); applicationComponent =
DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
Partial Mock
Application Module @Module public class ApplicationModule { @Provides public Info
providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
Application Module @Module public class ApplicationModule { // removed }
API Module @Module public class ApiModule { @Provides public Info
providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
API Module @Module public class ApiModule { @Provides public Info
providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
API Module @Module public class ApiModule { @Provides public Info
providesInfo() { return Info.newInstance(); } @Provides public Auth providesAuth() { return Auth.newInstance(); } }
API Component @Component(modules = ApiModule.class) public interface ApiComponent { Info
getInfo(); Auth getAuth(); }
API Component @Component(modules = ApiModule.class) public interface ApiComponent { Info
getInfo(); Auth getAuth(); }
API Component @Component(modules = ApiModule.class) public interface ApiComponent { Info
getInfo(); Auth getAuth(); }
API Component @Component(modules = ApiModule.class) public interface ApiComponent { Info
getInfo(); Auth getAuth(); } /FDFTTBSZ
Component Dependency
Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent
plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
Application Component @Component(modules = ApplicationModule.class) public interface ApplicationComponent { ActivityComponent
plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
Application Component @Component(modules = ApplicationModule.class, dependencies = ApiComponent.clas) public interface
ApplicationComponent { ActivityComponent plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
Application Component @Component(modules = ApplicationModule.class, dependencies = ApiComponent.clas) public interface
ApplicationComponent { ActivityComponent plus(ActivityModule module); void inject(Foo foo); void inject(Bar bar); }
Custom Application
My Application @Override public void onCreate() { super.onCreate(); applicationComponent =
DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); } public ApplicationComponent getApplicationComponent() { return applicationComponent; }
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; }
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; }
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; }
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; }
Mock API Module
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; } }
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; } }
Mock API Component
Mock API Component @Component(modules = MockApiModule.class) public interface MockApiComponent extends
ApiComponent { }
Mock API Component @Component(modules = MockApiModule.class) public interface MockApiComponent extends
ApiComponent { }
Mock API Component @Component(modules = MockApiModule.class) public interface MockApiComponent extends
ApiComponent { }
Test Application
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(); } }
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(); } }
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(); } }
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(); } }
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; } }
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; } }
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; } }
Mock Test Runner
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); } }
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); } }
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); } }
Write Your Test!
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() { } }
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() { } }
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() { } }
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() { } }
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() { } }
Use Your Runner android { defaultConfig { testInstrumentationRunner "your.package.runner.MockTestRunner" }
}
Thank you!