1

I cannot use the feature with OutputCapture within JUnit testing. I want to use this feature to see some log messages. If I tried to include the class package org.springframework.book.test.rule.OutputCapture in IDEA it shows me warn message "No suggestions". So that I am not able to access to outputCapture class (even test package is not visible).

As I found out on this website this class is supported since version 1.3. However I am using correct spring boot version 1.5.

import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.rule.OutputCapture; THIS IS NOT RECOGNIZED

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

public class MyTest {

    @Rule
    public OutputCapture capture = new OutputCapture();

    @Test
    public void testName() throws Exception {
        System.out.println("Hello World!");
        assertThat(capture.toString(), containsString("World"));
    }

}

What is wrong?

Luke
  • 1,163
  • 2
  • 11
  • 19

2 Answers2

3

The OutputCapture class was deprecated in version 2.2.0 in Spring Boot test in favour of the OutputCaptureRule class.

See this answer for how to use OutputCaptureRule class.

Joman68
  • 2,248
  • 3
  • 34
  • 36
0

Just a wide guess since I don't know your dependencies: if you are using maven, try to add the following dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.1.6.RELEASE</version> <!-- or another version -->
    <scope>test</scope>
</dependency>

The reason is that OutputCapture comes along from the project spring-boot-test.

Github: OutputCapture

KeyMaker00
  • 6,194
  • 2
  • 50
  • 49