Jacobs2000
Jacobs2000

Reputation: 938

SpingBoot OutputCaptureRule is throwing IllegalStateException

I am following instructions in https://www.docs4dev.com/javadoc/en/org/springframework/boot/spring-boot-test/2.2.2.RELEASE/org/springframework/boot/test/system/OutputCaptureRule.html

I am using Maven with spring-boot-starter-parent version 2.2.2.

My test is very simple:

@SpringBootTest(classes = MyApplication.class) // this loads Springboot context
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@ContextConfiguration(classes = {MyTestConfig.class, AnotherTestConfig.class})
public class MyTest {


    @Rule
    public OutputCaptureRule output = new OutputCaptureRule();

    @Test
    public void theTest() {
        assertThat(output).contains("something");
    }

}

However, when I put a breakpoint in the assert line and try to evaluate output.getOut(), the result is: Method threw 'java.lang.IllegalStateException' exception. The details message is: No system captures found. Please check your output capture registration.

It seems like the feature is not working out of the box. Any idea what I am missing?

Upvotes: 0

Views: 1084

Answers (1)

Ken Chan
Ken Chan

Reputation: 90517

Most probably it is because you are using JUnit 5 as SpringBoot 2.2 provides JUnit 5 by default , but the OutputCaptureRule is the JUnit 4 's TestRule stuff and hence it cannot be activated under JUnit 5.

You should use the equivalent OutputCaptureExtension in JUnit 5 instead :

@SpringBootTest(classes = MyApplication.class) 
@ExtendWith(OutputCaptureExtension.class)
public class MyTest {

    @Test
    public void theTest(CapturedOutput output) {
        assertThat(output).contains("something");
    }

}

Upvotes: 1

Related Questions