Skip to content

Instantly share code, notes, and snippets.

@relax-more
Created October 9, 2015 07:27
Show Gist options
  • Save relax-more/ad70d56f3ff4dfe1ff2b to your computer and use it in GitHub Desktop.
Save relax-more/ad70d56f3ff4dfe1ff2b to your computer and use it in GitHub Desktop.
What is MockitoRule ?

The JUnit rule can be used instead of MockitoJUnitRunner. It requires JUnit at least 4.7. This rule adds following behavior: Initializes mocks annotated with Mock, so that explicit usage of MockitoAnnotations.initMocks(Object) is not necessary. Mocks are initialized before each test method. validates framework usage after each test method. See javadoc for Mockito.validateMockitoUsage().

Copy-paste from http://site.mockito.org/mockito/docs/current/org/mockito/junit/MockitoRule.html

public class ExampleTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
private List list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment