【问题标题】:How to apply two @RunWith in spring boot starter test如何在春季启动启动器测试中应用两个@RunWith
【发布时间】:2015-07-29 07:04:24
【问题描述】:

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

【问题讨论】:

    标签: java spring-boot junit4 parameterized-tests junitparams


    【解决方案1】:

    @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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-13
        • 1970-01-01
        • 2019-08-17
        • 2017-09-25
        • 1970-01-01
        • 1970-01-01
        • 2021-08-31
        • 2020-02-23
        相关资源
        最近更新 更多