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
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
460
testcontainers のススメ
sgash708
1
120
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
900
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
470
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
180
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
720
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
760
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
Effective Signals in Angular 19+: Rules and Helpers
manfredsteyer
PRO
0
100
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Become a Pro
speakerdeck
PRO
26
5k
Scaling GitHub
holman
458
140k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Being A Developer After 40
akosma
87
590k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
Music & Morning Musume
bryan
46
6.2k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
Visualization
eitanlees
146
15k
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!