Skip to content

Instantly share code, notes, and snippets.

@vexdev
Last active May 22, 2018 10:52
Show Gist options
  • Save vexdev/90cd69c0cd107e27a484 to your computer and use it in GitHub Desktop.
Save vexdev/90cd69c0cd107e27a484 to your computer and use it in GitHub Desktop.
Example Android Unit Test,
package com.example.android.testingfun;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberModifier.suppress;
/**
* Tests LaunchActivity in isolation from the system.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(LaunchActivity.class)
public class LaunchActivityTest {
private LaunchActivity tested;
@Mock
private Button mockButton;
@Before
public void setUp() throws Exception {
// We will Spy our tested activity so that we will be able to give it some input when
// for example the FindViewByID method is called.
tested = spy(LaunchActivity.class);
doReturn(mockButton).when(tested).findViewById(anyInt());
// We need to suppress methods from the Base Activity. This is why we need PowerMock, there
// are other ways but are more intrusive in our code.
suppress(method(Activity.class, "onCreate", Bundle.class));
suppress(method(Activity.class, "setContentView", int.class));
}
@Test
public void shouldSetupListener() throws Exception {
// When we call the onCreate...
tested.onCreate(mock(Bundle.class));
// Then the setOnClickListener method will be called.
verify(mockButton).setOnClickListener((View.OnClickListener) any());
}
@Test
public void shouldStartActivity() throws Exception {
// Let's setup our mockButton so that it will capture the listener
ArgumentCaptor<View.OnClickListener> captor =
ArgumentCaptor.forClass(View.OnClickListener.class);
doNothing().when(mockButton).setOnClickListener(captor.capture());
// When we call the onCreate...
tested.onCreate(mock(Bundle.class));
// Listener should be in place (Was tested before) so now we need to check that it starts
// an activity.
doNothing().when(tested).startActivity((Intent) any());
doNothing().when(tested).finish(); // We should also test this to be called in a new test.
captor.getValue().onClick(mockButton);
verify(tested).startActivity((Intent) any());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment