rohit
rohit

Reputation: 902

How to apply two @RunWith in spring boot starter test

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.

Upvotes: 4

Views: 3951

Answers (2)

wjans
wjans

Reputation: 10115

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

Upvotes: 1

Alex Borysov
Alex Borysov

Reputation: 281

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

Upvotes: 5

Related Questions