Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConsoleCaptor #132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

Hakky54
Copy link
Contributor

@Hakky54 Hakky54 commented Jan 11, 2023

This pull request is similar to this one: #131

LogCaptor is focussed on capturing data from a Logger (Java Util Logging, SLF4J, Log4J2) which includes log level based messages, excpetions, unformatted and formatted logs etc. While ConsoleCaptor will just capture everything which is printed to the console either by using System.out or Logger output to the console.

ConsoleCaptor is designed to easily capture everything from the console output for assertions. It takes away the verbose setup of preparing everything and also properly closing the streams and reverting the temporally configuration under the covers. The end-user may have something similar to this setup:

public class FooService {

    public void sayHello() {
        System.out.println("Keyboard not responding. Press any key to continue...");
        System.err.println("Congratulations, you are pregnant!");
    }

}

With the accompanied unit test with usage of ConsoleCaptor:

import static org.assertj.core.api.Assertions.assertThat;

import nl.altindag.console.ConsoleCaptor;
import org.junit.jupiter.api.Test;

public class FooServiceShould {

    @Test
    public void captureStandardAndErrorOutput() {
        ConsoleCaptor consoleCaptor = new ConsoleCaptor();

        FooService fooService = new FooService();
        fooService.sayHello();

        assertThat(consoleCaptor.getStandardOutput()).contains("Keyboard not responding. Press any key to continue...");
        assertThat(consoleCaptor.getErrorOutput()).contains("Congratulations, you are pregnant!");
        
        consoleCaptor.close();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant