ProductPromotion
Logo

Java

made by https://0x3d.site

GitHub - Hakky54/console-captor: 🎯 ConsoleCaptor captures console output for unit and integration testing purposes
🎯 ConsoleCaptor captures console output for unit and integration testing purposes - Hakky54/console-captor
Visit Site

GitHub - Hakky54/console-captor: 🎯 ConsoleCaptor captures console output for unit and integration testing purposes

GitHub - Hakky54/console-captor: 🎯 ConsoleCaptor captures console output for unit and integration testing purposes

Actions Status Quality Gate Status Coverage JDK Compatibility Kotlin Compatibility Scala Compatibility Android API Compatibility Reliability Rating Security Rating Vulnerabilities Apache2 license Maven Central javadoc FOSSA Status Join the chat at https://gitter.im/hakky54/consolecaptor

SonarCloud

ConsoleCaptor

Install library with:

Install with maven

<dependency>
    <groupId>io.github.hakky54</groupId>
    <artifactId>consolecaptor</artifactId>
    <version>1.0.3</version>
    <scope>test</scope>
</dependency>

Install with Gradle

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

<dependency org="io.github.hakky54" name="consolecaptor" rev="1.0.3" />

Table of contents

  1. Introduction
  2. Usage
  3. Contributing
  4. 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.

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 for all the scenario's.

Usage

Capture console output
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:
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:
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
  • Join the Gitter room and leave a feedback or help with answering users questions
  • Submit a PR

License

FOSSA Status

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory