????????????????????????????????
?????lovexiaov ???????[ 2016/5/27 10:35:27 ] ??????????????? ???????
???????????????á?
?????????????????? Reddit ?? Robolectric and Dagger 2 ??????????????????????????????????????????????????????
???????????DI????? & ???????
????????????У????????????????????????????????????????????????RestApi??DataManager??????????????????????????????????“???”????User??Message??????????????????????????????????
????????ζ???????????????????????????? DI ????????????????????????????????????????????????????????????·???????????
????· ???????????????????
????· ????????????
????99% ?????????????? DI ????????????Activity??Fragment??View??Service???????????????????????????????? DI ???????????????????? MVP ??????????? Android ??????????????????ù????UI????????????????????????????
????DI ??? & ???????
??????????????????????????????? DI ?????????????????????????????????????????????ɡ????????????????????????DI-friendly????????????? DI ?????????????????
????????????????? DI ????????????????????????? Robolectric??????????????
????DI ??? & ?????UI??????
??????????????????? DI ???????????????????????????????????????????????????????
??????????????instrumentation?????Espresso??Robotium???????? instrumentation ??????????? DI ????????????????
?????????? Dagger 2 ?? Robolectric ???????????????????
????(???????????????)
????????·?????? Roboletric ?????????????????Application?????????????????
??????????? application ???ж??????????DaggerAppComponent???????Builder??????????????????????????????????? application ??
????application ??
public class MyApp extends Application {
@NonNull // Initialized in onCreate.
AppCompontent appComponent;
@Override
public void onCreate() {
appComponent = prepareAppComponent().build();
}
// Here is the trick?? we allow extend application class and modify AppComponent.
@NonNull
protected DaggerAppComponent.Builder prepareAppComponent() {
return new DaggerAppComponent.Builder();
}
}
??????????????? application ??
public class MyIntegrationTestApp extends MyApp {
@Override
@NonNull
protected DaggerAppComponent.Builder prepareAppComponent() {
return super.prepareAppComponent()
.someModule(new SomeModule() {
@Override
public SomeDependency provideSomeDependency(@NonNull SomeArgs someArgs) {
return mock(SomeDependency.class); // You can provide any kind of mock you need.
}
})
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11