Can I assert on Lombok's @Slf4j logger?

51 Views Asked by At

Suppose I want to test that this method prints a log.warn message if the Flux is empty

@Slf4j
class SomeClass {
    void someMethod(Flux<Integer> intFlux) {
        intFlux.switchIfEmpty(Mono.defer(() -> {
            log.warn("No integers!");
            return Mono.empty();
        }))
        .subscribe(/* consume */);
    }
}

How can I do it? Specifically, how can I assert on Lombok's logger, if it's at all possible? It's not passed into the constructor, I can't mock() and verify(). Is it the only option to simply get rid of Lombok?

1

There are 1 best solutions below

2
Bohemian On

Intercept sysout:

// pre test
PrintStream oldOut = System.out;
ByteArrayOutputStream capturedOutput = new ByteArrayOutputStream();
PrintStream newOut = new PrintStream(capturedOutput);
System.setOut(newOut);
// run tests
newOut.flush();
// restore sysout
System.setOut(oldOut);
String output = capturedOutput.toString();
// assert 'output' contains stuff