Skip to content

Instantly share code, notes, and snippets.

@bademux
Created November 6, 2015 15:38
Show Gist options
  • Save bademux/8a955935631a45601964 to your computer and use it in GitHub Desktop.
Save bademux/8a955935631a45601964 to your computer and use it in GitHub Desktop.
Mockito Parametrized UnitTest, MockitoClassRunnerFactory usage example
@Parameterized.UseParametersRunnerFactory(MockitoClassRunnerFactory.class)
@RunWith(Parameterized.class)
public class JobServiceSetStatusCheckIsManualTest {
@Parameterized.Parameter
public State state;
@Parameterized.Parameters(name = "{index}: expected {0}")
public static Collection<?> data() {
return Arrays.asList(new Object[][]{{"test"}});
}
@Test
public final void test() {
assertEquals(state, service.getState());
}
@Before
public void setUp() {
when(service.getState(state)).thenReturn(state);
}
@After
public void tearDown() {
reset(service);
}
@Mock
SomeService service;
}
class MockitoClassRunnerFactory implements ParametersRunnerFactory {
@Override
public Runner createRunnerForTestWithParameters(final TestWithParameters test) throws InitializationError {
return new BlockJUnit4ClassRunnerWithParameters(test) {
@Override
public Object createTest() throws Exception {
final Object testInstance = super.createTest();
MockitoAnnotations.initMocks(testInstance);
return testInstance;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment