Created
November 6, 2015 15:38
-
-
Save bademux/8a955935631a45601964 to your computer and use it in GitHub Desktop.
Mockito Parametrized UnitTest, MockitoClassRunnerFactory usage example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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