Android????????????????????????????
???????С?? ???????[ 2016/8/26 14:38:16 ] ??????????? ???????
??????????????????£????????Dagger2????????????С????dagger2?????????????Dagger2??????????????????Module ??Component??Module?????????????DataModel???????????????DataActivity???????????????????????????????????DataModel??Dependency???????????????DataActivity??Client????Component?????Client???Dependency??????????????DataActivity???Component??????????DataModel???????
?????????????????????Component???????????dependency?????????????????????????dependency??????Module???????????Component??????Module???????Module???????????dependency??????????????棬?????????????Module????????????DataModel?????????????У?????д???TestingModule???????????????Module?????override??????DataModel???????????????mock??DataModel??????????????????????TestingModule??????Component???????????DataActivity???Component?????DataModel??????mock??????DataModel????
???????????????????production code??????????testing?????κζ????????????????????????????????
????Robolectric?????Android????????????
??????????????Android????????????????JVM???????д?JUnit????????????????Android???????????????????????????????????????????????????Щ???????з???????????throw new RuntimeException("stub");??????????????????????????????????????????????????????RuntimeException("Stub")??
????????????????????????????????
????1?????Android????Instrumentation???????????????????????????????????????
????2?????????????????MVP???????????????????????м??Presenter??Model???java???????????JVM????????View??????android???????????
????3?????Robolectric???????????????????????JVM???????????????????????????????????????????????Щ???????????????????????????????????????????JVM??????????????????????e???????????
??????????????work???????????????????????????ε??????????????????????????apk???????????????????????????????app??????????????????????е????????????TDD???????????????????
????????????????????Robolectric?????????????1. ??????????????б???????????android????java????????????????2. ??????????????????????????????????View??????????????????????????Robolectric????????????????????????java?????????????????e????????????????java?????????????????????????????????????java?????????治?????????????????????????????????????Robolectric???????Robolectric????????Instrumentation testing????????????????????merge?Щ?????build???????????app???????????java??JUnit??????????????????????
?????t??????????????????
????· ????Instrumentation testing????????????app???С
????· Robolectric??10??????
????· JUnit???????????
???????????????????Robolectric??10????????????????????app???????????????????????????Robolectric????????OK???
?????????????????????????????????????????????JUnit4 + Mockito + Dagger2 + Robolectric???????????????????????????????????
???????????????
????????????????????????????????????????£????????????app?????????????????
?????????????????????????????
????????Activity?????CheckoutActivity??????????????CheckoutActivity????????CheckoutModel??loadCheckoutData()??????????????????????????????????????????????????????Api??(mApi)??get???????????????Api?????callback?????callback???????????????????Otto Bus(mBus) post?????CheckoutActivity????Subscribe?????Event??????????onCheckoutDataLoaded()??????????Event????????????????????????
?????????д???£?
public class CheckoutActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// other code?? like setContentView?? get data from Intent?? etc.
mCheckoutModel.loadCheckoutData(paymentId);
}
@Subscribe
public void onCheckoutDataLoaded(DataLoadedEvent event) {
if (event.successful()) {
//Get data from event and update UI
} else {
//show error message
}
}
}
public class CheckoutModel {
public void loadCheckoutData(String paymentId) {
//Other code?? like composing params
mApi.get(someUrl?? someParams?? new NetworkCallback() {
@Override
public void onSuccess(Object data) {
mBus.post(new DataLoadedEvent(data));
}
@Override
public void onFailure(int code?? String msg) {
mBus.post(new DataLoadedEvent(code?? msg));
}
});
}
}
????????CheckoutActivity?????mCheckoutModel??CheckoutModel?????mApi??CheckoutModel?????mBus?????????Dagger2?????????????????????????Щ????mock??
??????
???·???
??????????????????
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