I am trying to write this test in android testing framework to run on emulator since i can't use robolectric in my project for some compatibility reasons.
How do i switch and what do i use (ActivityInstrumentationTestCase2, ActivityTestCase)?
Maybe some example?
I am trying to convert test from this project, and here is the code
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>> cb;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ActivityController 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 testRepos = new ArrayList();
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 noRepos = new ArrayList();
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");
}
}