Skip to content

Added ConsoleCaptor#949

Merged
akullpp merged 2 commits into
akullpp:masterfrom
Hakky54:master
Sep 8, 2021
Merged

Added ConsoleCaptor#949
akullpp merged 2 commits into
akullpp:masterfrom
Hakky54:master

Conversation

@Hakky54

@Hakky54 Hakky54 commented Sep 7, 2021

Copy link
Copy Markdown
Contributor

This pull request is similar to this one: #869

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();
    }
}

@akullpp akullpp merged commit 9de6ea4 into akullpp:master Sep 8, 2021
@Hakky54

Hakky54 commented Sep 8, 2021

Copy link
Copy Markdown
Contributor Author

Thank you for adjusting the pr and merging it! I did made couple of typos but you catched it 👊👍

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.

2 participants