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
flutterkaigi_2024.pdf
kyoheig3
0
120
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
初めてDefinitelyTypedにPRを出した話
syumai
0
410
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
180
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Site-Speed That Sticks
csswizardry
0
25
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
How STYLIGHT went responsive
nonsquared
95
5.2k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
KATA
mclloyd
29
14k
A designer walks into a library…
pauljervisheath
204
24k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Become a Pro
speakerdeck
PRO
25
5k
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!