Skip to content

NPE with Junit5, SLF4J (log4j2) and Spring Boot Test #6

@vguna

Description

@vguna

Just tried the following:

@ExtendWith(SpringExtension.class)
@SpringBootTest(
        classes = { Bla.class },
        webEnvironment = WebEnvironment.NONE)
@ActiveProfiles("test")
@LogCollectorExtension
public class FooTest {

...
    Logger logger = LoggerFactory.getLogger(Foo.class);
    JUnit5LogCollector collector = new JUnit5LogCollector(logger);
...

    @Test
    public void testFoo() {

        doSomethingWithFoo();

        // NPE happens here
        collector.getLogs().stream().forEach(log -> System.out.println(log));
    }
...
}

It dies with:

java.lang.NullPointerException
	at dk.bitcraft.lc.Log4j2Collector.getResult(Log4j2Collector.java:41)
	at dk.bitcraft.lc.JUnit5LogCollector.getLogs(JUnit5LogCollector.java:28)
	at com.acme.FooTest.testFoo(FooTest.java:131)

Seems like the appender in Log4j2Collector isn't properly initialized. Maybe somehow setup() isn't called properly?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions