4

我正在使用 spring boot 启动器测试来编写 JUnit 测试用例。我很想使用 JunitParamrunner,它有助于为参数化测试传递文件。基本上它从文件中逐行读取数据,并且每行调用一个测试用例。问题是同时使用我需要通过 @RunWith 和 SpringJUnit4ClassRunner 以及 JUnitParamsRunner。我不知道该怎么做。任何人都可以提供一些线索。

4

2 回答 2

5

@wjans 提到的 SpringClassRule 如果是最好的解决方案,但是如果您的 Spring 版本小于 4.2(最新的 spring-boot-starter-test 取决于 spring 版本 4.1.7),您可以在测试构造函数中初始化上下文:

@ContextConfiguration(<your context location>)
@RunWith(JUnitParamsRunner.class)
public class ParameterizedTestWithSpring {

    private TestContextManager testContextManager;

    public ParametrizedTestWithSpring() throws Exception {
        this.testContextManager = new TestContextManager(getClass());
        this.testContextManager.prepareTestInstance(this);
    }

    // your test methods
于 2015-07-29T08:57:15.963 回答
1

您可以尝试通过使用SpringClassRule而不是@RunWith注释来实现您的 spring 集成。

于 2015-07-29T08:42:13.753 回答