Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active April 5, 2017 02:41
Show Gist options
  • Save pokk/fbdb831a78a890651ccbaa480b4a3696 to your computer and use it in GitHub Desktop.
Save pokk/fbdb831a78a890651ccbaa480b4a3696 to your computer and use it in GitHub Desktop.
Android unit test

[TOC]

Mockito

Generating a kind of virtual object for verifying the method reacting output which fits our expecting.

Concept

  1. Verifying the using status of a assignment object's method and how many times was executed, and what's the parameters, etc.

  2. Assigning the action of a object's method or specific return value, or specific executing action, etc.

Active Mockito Annotations

There are three ways for activing the Mockito annotations such as @Mock, @Spy, @InjectMocks, etc.

  • Annotating the JUnit test class.
@RunWith(MockitoJUnitRunner.class)
    public class TestClass {
}
  • Invoking it in the @Before method.
public class TestClass {
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }
}
  • Using a Mockito JUnit rule.
public class TheTest {
    @Rule public MockitoRule mockito = MockitoJUnit.rule();
}

Robolectric

Active Rebolectric Annotations

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class TestClass {
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment