https://github.com/hakky54/console-captor
🎯 ConsoleCaptor captures console output for unit and integration testing purposes
https://github.com/hakky54/console-captor
Last synced: 7 months ago
JSON representation
🎯 ConsoleCaptor captures console output for unit and integration testing purposes
- Host: GitHub
- URL: https://github.com/hakky54/console-captor
- Owner: Hakky54
- License: apache-2.0
- Created: 2021-07-14T16:22:15.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-02-10T12:52:48.000Z (9 months ago)
- Last Synced: 2025-03-27T23:23:24.050Z (7 months ago)
- Language: Java
- Homepage:
- Size: 88.9 KB
- Stars: 30
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/Hakky54/console-captor/actions)
[](https://sonarcloud.io/dashboard?id=io.github.hakky54%3Aconsolecaptor)
[](https://sonarcloud.io/dashboard?id=io.github.hakky54%3Aconsolecaptor)
[](#)
[](#)
[](#)
[](#)
[](https://sonarcloud.io/dashboard?id=io.github.hakky54%3Aconsolecaptor)
[](https://sonarcloud.io/dashboard?id=io.github.hakky54%3Aconsolecaptor)
[](https://sonarcloud.io/dashboard?id=io.github.hakky54%3Aconsolecaptor)
[](https://github.com/Hakky54/console-captor/blob/master/LICENSE)
[](https://mvnrepository.com/artifact/io.github.hakky54/consolecaptor)
[](https://javadoc.io/doc/io.github.hakky54/consolecaptor)
[](https://app.fossa.io/projects/git%2Bgithub.com%2FHakky54%2Fconsole-captor?ref=badge_shield)
[](https://gitter.im/hakky54/consolecaptor?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://sonarcloud.io/dashboard?id=io.github.hakky54%3Aconsolecaptor)
# ConsoleCaptor
# Install library with:
### Install with [maven](https://mvnrepository.com/artifact/io.github.hakky54/consolecaptor)
```xml
io.github.hakky54
consolecaptor
1.0.3
test
```
### Install with Gradle
```groovy
testImplementation 'io.github.hakky54:consolecaptor:1.0.3'
```
### Install with Scala SBT
```
libraryDependencies += "io.github.hakky54" % "consolecaptor" % "1.0.3" % Test
```
### Install with Apache Ivy
```xml
```
## Table of contents
1. [Introduction](#introduction)
- [Advantages](#advantages)
- [Tested Java versions](#tested-java-versions)
2. [Usage](#usage)
- [Capture Console Output](#capture-console-output)
- [Reuse ConsoleCaptor for multiple tests](#initialize-consolecaptor-once-and-reuse-it-during-multiple-tests-with-clearoutput-method-within-the-aftereach-method)
3. [Contributing](#contributing)
4. [License](#license)
# Introduction
Hey, hello there 👋 Welcome. I hope you will like this library ❤️
ConsoleCaptor is a library which will enable you to easily capture the output of the console for unit testing purposes.
Do you want to capture logs? Please have a look at [LogCaptor](https://github.com/Hakky54/log-captor).
### Advantages
- No mocking required
- No custom JUnit extension required
- Plug & play
- Zero transitive dependencies
### Tested Java versions
- Java 8
- Java 11+
See the unit test [ConsoleCaptorShould](src/test/java/nl/altindag/console/ConsoleCaptorShould.java) for all the scenario's.
# Usage
##### Capture console output
```java
public class FooService {
public void sayHello() {
System.out.println("Keyboard not responding. Press any key to continue...");
System.err.println("Congratulations, you are pregnant!");
}
}
```
###### Unit test:
```java
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();
}
}
```
##### Initialize ConsoleCaptor once and reuse it during multiple tests with clearOutput() method within the afterEach method:
```java
import nl.altindag.console.ConsoleCaptor;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
public class FooServiceShould {
private static ConsoleCaptor consoleCaptor;
@BeforeAll
public static void setupConsoleCaptor() {
consoleCaptor = new ConsoleCaptor();
}
@AfterEach
public void clearOutput() {
consoleCaptor.clearOutput();
}
@AfterAll
public static void tearDown() {
consoleCaptor.close();
}
@Test
public void captureStandardOutput() {
FooService service = new FooService();
service.sayHello();
assertThat(consoleCaptor.getStandardOutput()).contains("Keyboard not responding. Press any key to continue...");
}
@Test
public void captureErrorOutput() {
FooService service = new FooService();
service.sayHello();
assertThat(consoleCaptor.getErrorOutput()).contains("Congratulations, you are pregnant!");
}
}
```
# Contributing
There are plenty of ways to contribute to this project:
* Give it a star
* Make a donation through [GitHub](https://github.com/sponsors/Hakky54) or [open collective](https://opencollective.com/hakky54)
* Join the [Gitter room](https://gitter.im/hakky54/consolecaptor) and leave a feedback or help with answering users questions
* Submit a PR
## License
[](https://app.fossa.io/projects/git%2Bgithub.com%2FHakky54%2Fconsole-captor?ref=badge_large)