Last active
August 15, 2018 08:26
-
-
Save bademux/5ed07e564c879994b93c to your computer and use it in GitHub Desktop.
Spring Parametrized Integration Test, SpringJUnit4ClassRunnerFactory usage example
This file contains hidden or 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(SpringJUnit4ClassRunnerFactory.class) | |
@RunWith(Parameterized.class) | |
@SpringTestConfiguration | |
@Transactional | |
public class StringParametrizedIntegrationTest { | |
@Parameterized.Parameter | |
public String uri; | |
@Parameterized.Parameters(name = "{index}: for {0} ") | |
public static Collection<Object[]> data() { | |
return Arrays.asList(new Object[][]{ | |
{"test1"}, | |
{"test2"} | |
}); | |
} | |
@Autowired | |
Helper helper; | |
@Test | |
public void test() throws Exception { | |
assertThat(uri, helper.loadArray(uri), arrayWithSize(greaterThan(0))); | |
} | |
} | |
class SpringJUnit4ClassRunnerFactory implements ParametersRunnerFactory { | |
@Override | |
public Runner createRunnerForTestWithParameters(final TestWithParameters test) throws InitializationError { | |
final LocalBlockJUnit4ClassRunnerWithParameters testRunner = new LocalBlockJUnit4ClassRunnerWithParameters(test); | |
return new SpringJUnit4ClassRunner(test.getTestClass().getJavaClass()) { | |
@Override | |
protected Object createTest() throws Exception { | |
final Object testInstance = testRunner.createTest(); | |
getTestContextManager().prepareTestInstance(testInstance); | |
return testInstance; | |
} | |
@Override | |
protected String getName() { | |
return testRunner.getRealName(); | |
} | |
}; | |
} | |
private class LocalBlockJUnit4ClassRunnerWithParameters extends BlockJUnit4ClassRunnerWithParameters { | |
public LocalBlockJUnit4ClassRunnerWithParameters(final TestWithParameters test) throws InitializationError { | |
super(test); | |
} | |
String getRealName() { | |
return getName(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great!