繁体   English   中英

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM