![](/img/trans.png)
[英]java sbt test: exception ignored if test is run with SpringJUnit4ClassRunner
[英]SpringJUnit4ClassRunner: run test method multiple times with different input data
我正在为Spring应用程序开发测试。 我有几个测试,我想多次运行不同的数据。
我不能使用JUnit的@Parameters,因为必须使用Parameterized.class运行测试类才能使其工作,而我必须使用设置模拟Spring上下文的SpringJUnit4ClassRunner.class运行测试类。 不幸的是,这个运行器似乎不支持处理@Parameters注释 。 我还看了一下TestContextBootstrapper和TextExecutionListener,但它似乎也无法帮助我。
有没有办法针对不同的输入数据多次运行Spring应用程序的测试?
我需要类似于TestNG的@Test(dataProvider=) @DataProvider
couple或@Factory(dataProvider = ) @DataProvider
couple。
提前致谢。
如果您可以依赖于JUnit 4.12,则可以使用@UseParametersRunnerFactory
Parameterized
@UseParametersRunnerFactory
首先,创建一个实现ParameterizedRunnerFactory
的类,返回要运行测试的Runner
实例:
public class SpringJUnit4ClassRunnerFactory
implements ParameterizedRunnerFactory {
@Override
public Runner createRunnerForTestWithParameters(final TestWithParameters test)
throws InitializationError {
return new SpringJUnit4ClassRunner(testClass.getJavaClass()) {
@Override
protected Object createTest() throws Exception {
Object[] args = test.getParameters().toArray();
Object testInstance = test.getTestClass().getOnlyConstructor()
.newInstance(args);
// copied from SpringJUnit4ClassRunner.createTest():
getTestContextManager().prepareTestInstance(testInstance);
return testInstance;
}
};
}
}
然后,您可以使用@UseParametersRunnerFactory
注释您的测试类:
@UseParametersRunnerFactory
@RunWith(Parameterized.class)
public class FooTesdt {
}
有关详细信息,请参阅参数化Javadoc 。
在这里找到一个更简单的解决方案。
将发布哪些解决方案对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.