Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Test with Spring and Junit5 without context creation

Tags:

spring

junit5

We recently migrated our test framework to JUnit5 and are having some issues using @Disabled (und ExecutionConditions) in tests which are using @SpringJUnitConfig: In Junit4 @Ignore disabled the test execution without question and without executing anything. With Junit5 and @Disabled, the system now creates the spring context before realizing that the test should not be executed. In our case this leads to disabled tests failing, because some of them are disabled because the context cannot be created under some circumstances.

Is it possible to disable a test (class) in JUnit5 in a way that no spring context is created for this test?

Minimal example:

@TestInstance(Lifecycle.PER_CLASS)
@RunWith(JUnitPlatform.class) // To support tests running in eclipse 
@SpringJUnitConfig(classes = {BaseTestSpringTest.TestConfiguration.class})
@Disabled
public class BaseTestSpringTest {

    @Configuration
    @ComponentScan(basePackages = { "my.package" })
    public class TestConfiguration {
    }

    @Component
    public class TestClass {
        @PostConstruct
        public void fail() {
            throw new IllegalStateException();
        }
    }

    @Autowired
    protected TestClass test;

    @Test
    public void test() throws Exception {

    }
}
like image 982
Nitek Avatar asked Oct 09 '17 06:10

Nitek


1 Answers

This is a bug in JUnit Jupiter that only occurs when using the PER_CLASS test instance lifecycle mode.

I have raised the following issue in the JUnit 5 issue tracker to address this issue. https://github.com/junit-team/junit5/issues/1103

like image 118
Sam Brannen Avatar answered Oct 20 '22 20:10

Sam Brannen