????Robolectric??y???
????Robolectric????????д??

??????????????д???????test?????????androidTest????????????л???????????????????????????????Java???????????????λ??д????е?С??λ???????????????????????case???????@Test???????????????????????case??????Robolectric?????У???????????????@RunWith(RobolectricTestRunner.class)??????Robolectric?????????£?
????//??????import
????@RunWith(RobolectricTestRunner.class)
????public class MainActivityTest {
????@Before
????public void setUp() {
????//??г?????????
????}
????@Test
????public void testCase() {
????//??и??????????ж?
????}
????}
???????????У?????@Before???????????????????????????У????????????Щ???????????????繹??????????????Activity??????@test?????????????case????Robolectric??У???Щcase???????????????????????????е????????case??????????????case??????????case?????????????????????????????????????????
????????Robolectric?÷?
????Robolectric??????????Χ??Activity???????Activity??View?????????????Fragment??View?????????????????????Robolectric??????Toast??Dialog??????????????????????????Robolectric????????????????response???????ЩRobolectric???????????????ConcurrentTask??????????????Shadow???????????????潫???????Robolectric??????÷???
????Robolectric 2.4???????????
???????????App?????Activity???????????????????????????????????????????App??????????飬?????????????????Activity?????????@Before???????????????????????????????????????????????????????£?
????public void prepareHttpResponse(String filePath) throws IOException {
????String netData = FileUtils.readFileToString(FileUtils.
????toFile(getClass().getResource(filePath))?? HTTP.UTF_8);
????Robolectric.setDefaultHttpResponse(200?? netData);
????}//??????????Robolectric 2.4??3.0??????????????????λ??
????????Robolectric 2.4???????????????????????????????????????????????????????????filePath?????????????????·????setDefaultHttpResponse()????????????Response????????????к????????????????????????????????????????????????????к?????Activity???????????Activity??
??????Robolectric 3.0?????£?????????????????????????????????????????????????mock???????????????????????????繃????
????Activity???????????????
?????????????????????????Activity?????????????£?
????@Test
????public void testSampleActivity(){
????SampleActivity sampleActivity??Robolectric.buildActivity(SampleActivity.class).
????create().resume().get();
????assertNotNull(sampleActivity);
????assertEquals("Activity?????"?? sampleActivity.getTitle());
????}
????Robolectric.buildActivity()???????Activity??create()??????к???Activity?????е?onCreate?????resume()????onResume?????assertNotNull??assertEquals??JUnit?е?????Robolectric??????л?????????ж???????????JUnit?е?????
????Activity?????Android?????????????????????????£?
????@Test
????public void testActivityTurn(ActionBarActivity firstActivity?? Class secondActivity) {
????Intent intent = new Intent(firstActivity.getApplicationContext()?? secondActivity);
????assertEquals(intent?? Robolectric.shadowOf(firstActivity).getNextStartedActivity());//3.0??API??2.4???
????}
????Fragment?????л?
????Fragment??Activity??????????Robolectric??????Activity?????У????????????????????е?Fragment????????Fragment??????Activity?С?
??????????Fragment????????????????Activity?е?Fragment????????????onResume??Σ???Activity??Robolectric???resume()????????Activity?в?????????Fragment??????Robolectric???????Fragment????????£?
????@Test
????public void addfragment(Activity activity?? int fragmentContent){
????FragmentTestUtil.startFragment(activity.getSupportFragmentManager().findFragmentById(fragmentContent));
????Fragment fragment = activity.getSupportFragmentManager().findFragmentById(fragmentContent);
????assertNotNull(fragment);
????}
????startFragment()????????????????????fragment??????л????Fragment??????Activity?е??????????????Activity?????á?
?????????????????????
????@Test
????public void testButtonClick(int buttonID){
????Button submitButton = (Button) activity.findViewById(buttonID);
????assertTrue(submitButton.isEnabled());
????submitButton.performClick();
????//???????????
????}
????????????????????performClick()??????????????????????ListView?????漰??Adapter?????????????д?????£?
????//listView???????
????listView.performItemClick(listView.getAdapter().getView(position?? null?? null)?? 0?? 0);
??????button???????в????
????Dialog??Toast????
????????Dialog??Toast????????£?
????public void testDialog(){
????Dialog dialog = ShadowDialog.getLatestDialog();
????assertNotNull(dialog);
????}
????public void testToast(String toastContent){
????ShadowHandler.idleMainLooper();
????assertEquals(toastContent?? ShadowToast.getTextOfLatestToast());
????}
???????????????????Dialog??Toast?????????У????????Dialog??Toast???????
????Shadowд??????
????Robolectric?????????Java???л????£?????Shadow??????Android?е?????????????????????Android?????????????ЩRobolectirc????????????????????????Shadow???????Robolectric??????
????@Implements(Point.class)
????public class ShadowPoint {
????@RealObject private Point realPoint;
????...
????public void __constructor__(int x?? int y) {
????realPoint.x = x;
????realPoint.y = y;
????}
????}//?????????Robolectric????
????????????У?@Implements??????Shadow?????@RealObject???????Android ????constructor?????Shadow?????????Shadow??????????Щ????????????????????ú???????????@Implementation??????????????Ч???Robolectric??????
????Shadow????????????Android??????к???????????????????Android????????????Shadow???????????????Android??????????????Android??????????????????Shadow?????config????????????д????@Config(shadows=ShadowPoint.class)??
????Mockд??????
?????????Щ?????????????????????????Mock??????????????????Mockito??????Mock???List???????????????????????·????
????List list = mock(List.class);   //mock?????????????????@mock??????????
???????????list???????????List???????????????????mock??List????????????????????????????ArrayList????????????????Shadow?????Mock?????????????????????????????????????????????Mock???????????????????????????????????????????????????й??????????????
????Mock?????Щ????JUnit??????????????????ú???????н??????????£?
????When(sample.dosomething()).thenReturn(someAction);//when(??????????).thenReturn(?????????????????????????);
????//??????????????sample.dosomething()???????????????sample.dosomething()??????????????someAction????????????????sample.dosomething()??????????
???????????????????????????????????????????????????????????????????????????????????????????溯??????????????????????????????????????
??????Mock???????????????????д????????????£?
????List list = mock(List.class);   //Mock??????????
????list.add(1);                    //??????????
????verify(list).add(1);            //???????????????
????verify(list??time(3)).add(1);    //??????????????д???
???????Щ????????????????????У??????Mock???????retrofit??????????????????£?
//????ο???ο?????[3]
public class MockClient implements Client {
@Override
public Response execute(Request request) throws IOException {
Uri uri = Uri.parse(request.getUrl());
String responseString = "";
if(uri.getPath().equals("/path/of/interest")) {
responseString = "?????json1";//?????????÷????
} else {
responseString = "?????json2";
}
return new Response(request.getUrl()?? 200?? "nothing"?? Collections.EMPTY_LIST?? new TypedByteArray("application/json"?? responseString.getBytes()));
}
}
//MockClient??÷?????£?
RestAdapter.Builder builder = new RestAdapter.Builder();
builder.setClient(new MockClient());
????????????retrofit??response?????????????д???????????????????磬????????????繃??????????
?????????????????Robolectric???????????
?????????????Χ
??????Android????У?????????????????????????????????????????庯???????????????????????????????????????onStart()??onDestroy()??Щ???????????????????????????????????Scrum????????????????????δ???н??????д??????????????????????д????????
???????????????case?????????????????????????????case??????????????????????????????????У???????????????????????????????????????????????????????????????????????????????Щ???——?????????????????????????????????????
?????????????У????????????????д??????????????????????????????????????Χ????????????????????????????????????????????????????????????????£?????μ???????????????????????????????????????
?????????Android????????д???????4?????
????????Android???????????


????
?4 ????Android????????д????

?????????????????????????????????????????????????????????????