Skip to content

Instantly share code, notes, and snippets.

@colabug
Created July 23, 2012 20:03
Show Gist options
  • Save colabug/3165888 to your computer and use it in GitHub Desktop.
Save colabug/3165888 to your computer and use it in GitHub Desktop.
Chariot Blog Android Unit Testing With Robolectric
@Test
public void buttonClickShouldStartNewActivity() throws Exception {
button.performClick();
Intent intent = NewActivity.createIntent(activity);
assertThat(activity, new StartedMatcher(intent));
}
@Test
public void buttonClickShouldStartNewActivity() throws Exception {
button.performClick();
Intent intent = NewActivity.createIntent(activity);
assertThat(activity, new StartedMatcher(intent));
}
@Test
public void buttonClickShouldStartNewActivity() throws Exception {
Button button = activity.findViewById(R.id.next_screen_button);
button.performClick();
Intent intent = NewActivity.createIntent(activity);
assertThat(activity, new StartedMatcher(intent));
}
@Test
public void imageShouldEqualResourceDrawable() throws Exception {
ImageView image = (ImageView) activity.findViewById(R.id.sun_earth_image);
assertThat(image.getDrawable(),
equalTo(getResourceDrawable(R.drawable.sun_earth)));
}
@Test
public void shouldNotBeNull() throws Exception {
MyActivity activity = new MyActivity();
assertNotNull(activity);
}
@Test
public void shouldHaveWelcomeText() throws Exception {
TextView welcomeText = (TextView) activity.findViewById(R.id.welcome_text_view);
assertViewIsVisible(welcomeText);
}
public class ProjectTestRunner extends RobolectricTestRunner {
public ProjectTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
public static String getResourceString(int resourceId) {
return Robolectric.application.getApplicationContext().getString(resourceId);
}
public static Drawable getResourceDrawable(int resourceId) {
return Robolectric.application.getApplicationContext().getResources().getDrawable(resourceId);
}
public static void assertViewIsVisible(View view) {
assertNotNull(view);
assertThat(view.getVisibility(), equalTo(View.VISIBLE));
}
public static void assertViewIsHidden(View view) {
assertNotNull(view);
assertThat(view.getVisibility(), equalTo(View.GONE));
}
}
@Before
public void setUp() throws Exception {
activity = new MyActivity();
activity.onCreate(null);
image = (ImageView) activity.findViewById(R.id.sun_earth_image);
button = activity.findViewById(R.id.next_screen_button);
}
@Test
public void shouldHaveHiddenButton() throws Exception {
Button button = activity.findViewById(R.id.next_screen_button);
assertViewIsHidden(button);
}
@Test
public void shouldShowButtonAfterImageClick() throws Exception {
image.performClick();
assertViewIsVisible(button);
}
@Test
public void welcomeTextShouldEqualResource() throws Exception {
TextView welcomeText = (TextView) activity.findViewById(R.id.welcome_text_view);
assertThat(welcomeText.getText().toString(),
equalTo(getResourceString(R.string.WELCOME_STRING)));
}
@Test
public void welcomeTextClickShouldToastUser() throws Exception {
TextView welcomeText = (TextView) activity.findViewById(R.id.welcome_text_view);
welcomeText.performClick();
assertThat(getTextOfLatestToast(),
equalTo(getResourceString(R.string.WELCOME_TOAST)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment