How to apply two @RunWith in spring boot starter test

3.8k Views Asked by At

I am using spring boot starter test for writing JUnit test cases. I would love to use JunitParamrunner which facilitates passing files for parameterized test.Basically it reads data from file line by line and for every line invoke a test case. The issue is to use both I need to pass @RunWith with both SpringJUnit4ClassRunner as well as JUnitParamsRunner. I am not able to figure out how to do that. Can any one provide some leads.

2

There are 2 best solutions below

0
On BEST ANSWER

SpringClassRule mentioned by @wjans if the best solution, but if your Spring version is less than 4.2 (the latest spring-boot-starter-test depends on spring version 4.1.7), you can initialize the context in test constructor:

@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
0
On

You can try to achieve your spring integration by using the SpringClassRule instead of the @RunWith annotation.