CoreComponents icon indicating copy to clipboard operation
CoreComponents copied to clipboard

NestedRunner doesn't work with SpringClassRule and SpringMethodRule

Open BenoitAverty opened this issue 9 years ago • 1 comments

I'm trying to use the Spring Junit Rules (SpringClassRule and SpringMethodRule) with the NestedRunner, but I'm getting a NullPointerException at the start of the tests.

Here is a minimal example :

@RunWith(NestedRunner.class)
@SpringApplicationConfiguration(value=ExampleContext.class)
public class TestTest {

    @ClassRule
    public static final SpringClassRule springClassRule = new SpringClassRule();

    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Test
    public void firstTest() {
        org.junit.Assert.assertTrue(true);
    }

}
@Configuration
class ExampleContext {
}

and the error :

java.lang.NullPointerException
    at org.springframework.test.context.junit4.rules.SpringClassRule.apply(SpringClassRule.java:140)
    at org.junit.rules.RunRules.applyAll(RunRules.java:26)
    at org.junit.rules.RunRules.<init>(RunRules.java:15)
    at org.junit.runners.ParentRunner.withClassRules(ParentRunner.java:245)
    at org.junit.runners.ParentRunner.classBlock(ParentRunner.java:194)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:362)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

If I comment out the @RunWith annotation, then it works fine.

BenoitAverty avatar Apr 11 '16 13:04 BenoitAverty

Hi sorry for the long delay in answering.

This Runner was built a long time ago, and without @Rule support. Maybe you can get away with the new JUnit5 functionality? http://junit.org/junit5/docs/current/user-guide/#writing-tests-nested

Macroz avatar Sep 26 '16 06:09 Macroz