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
120
AWS Device FarmとCircleCIでAndroidのUIテストを自動化しよう
srym
1
4.9k
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
590
I/O 2017 Short Report
srym
0
300
Other Decks in Programming
See All in Programming
初めてDefinitelyTypedにPRを出した話
syumai
0
400
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
Realtime API 入門
riofujimon
0
150
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
as(型アサーション)を書く前にできること
marokanatani
9
2.6k
受け取る人から提供する人になるということ
little_rubyist
0
230
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
RubyLSPのマルチバイト文字対応
notfounds
0
120
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
KATA
mclloyd
29
14k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
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!