Robolectric对Android测试框架的改造测试

时间:2014-04-21 09:39:13

标签: android mockito robolectric android-testing retrofit

我正在尝试在Android测试框架中编写此测试以在模拟器上运行,因为出于某些兼容性原因我无法在我的项目中使用robolectric。

  1. 如何切换以及我使用什么(ActivityInstrumentationTestCase2, ActivityTestCase)?

  2. 也许是一些例子?

  3. 我正在尝试从此project转换测试,这是代码

    package com.swanson.octodroid.test;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.ArgumentCaptor;
    import org.mockito.Captor;
    import org.mockito.Mock;
    import org.mockito.Mockito;
    import org.mockito.MockitoAnnotations;
    import org.robolectric.Robolectric;
    import org.robolectric.shadows.ShadowToast;
    import org.robolectric.util.ActivityController;
    
    import retrofit.Callback;
    
    import com.swanson.octodroid.GitHub;
    import com.swanson.octodroid.MainActivity;
    import com.swanson.octodroid.Owner;
    import com.swanson.octodroid.Repository;
    
    import static org.fest.assertions.api.Assertions.assertThat; 
    import static org.fest.assertions.api.ANDROID.assertThat; 
    
    @RunWith(CustomTestRunner.class)
    public class MainActivityTest {
    
        private MainActivity activity;
    
        @Mock
        private GitHub mockApi;
    
        @Captor
        private ArgumentCaptor<Callback<List<Repository>>> cb;
    
        @Before
        public void setUp() {
            MockitoAnnotations.initMocks(this);
    
            ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class);
            activity = controller.get();
            activity.setApi(mockApi);
    
            controller.create();
        }
    
        @Test
        public void shouldFillAdapterWithReposFromApi() throws Exception {
            Mockito.verify(mockApi).repositories(Mockito.anyString(), cb.capture());
    
            List<Repository> testRepos = new ArrayList<Repository>();
            testRepos.add(new Repository("rails", "ruby", new Owner("dhh")));
            testRepos.add(new Repository("android", "java", new Owner("google")));
    
            cb.getValue().success(testRepos, null);
    
            assertThat(activity.getListAdapter()).hasCount(2);
        }
    
        @Test
        public void shouldToastSadMessageIfNoRepos() throws Exception {
            Mockito.verify(mockApi).repositories(Mockito.anyString(), cb.capture());
    
            List<Repository> noRepos = new ArrayList<Repository>();
    
            cb.getValue().success(noRepos, null);
    
            assertThat(ShadowToast.getTextOfLatestToast()).contains("No repos :(");
            assertThat(activity.getListAdapter()).isEmpty();
        }
    
        @Test
        public void shouldToastIfApiError() throws Exception {
            Mockito.verify(mockApi).repositories(Mockito.anyString(), cb.capture());
    
            cb.getValue().failure(null);
    
            assertThat(ShadowToast.getTextOfLatestToast()).contains("Failed");
        }
    
    }
    

0 个答案:

没有答案