Giter Club home page Giter Club logo

console-captor's Introduction

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

console-captor's People

Contributors

hakky54 avatar pierrebtz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

console-captor's Issues

ConsoleCaptor#clearOutput doesn't reset System.out and System.err

Describe the bug
The method ConsoleCaptor#clearOutput creates new streams but doesn't register them to System.out and System.err.
So subsequent calls to #getStandardOutput() and #getErrorOutput() always return empty lists.

To Reproduce

try (ConsoleCaptor console = new ConsoleCaptor()) {
    System.out.println("hello");
    // ok
    assertThat(console.getStandardOutput()).containsExactly("hello");

    console.clearOutput();

    System.out.println("world");
    // ko
    assertThat(console.getStandardOutput()).containsExactly("world");
}

getStandardOutput() not returning anything

Hello,

I am trying to use ConsoleCaptor to get logs from the console. I tried using LogCaptor, but it doesn't seem to work with my Maven repositories, so I resorted to ConsoleCaptor since it seems quick and easy to set up!

Background
I have a class that uses SL4J logging binded to a Log4J2 configuration. I have set up my log4j2.xml file with a Console Appender that logs error messages to the console. I know this works, because when I run the unit tests, I can see the log message printed in the console. I think the issue I am experiencing is ConsoleCaptor not picking up the log message from the console, because when I do a System.out.println(consoleCaptor.getStandardOutput()), nothing is returned.

My pom.xml file has the following dependencies:

<dependencies>
        <dependency>
            <groupId>io.github.hakky54</groupId>
            <artifactId>consolecaptor</artifactId>
            <version>1.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j18-impl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.0-alpha7</version>
        </dependency>

In my test file, I have set up ConsoleCaptor as follows:

@BeforeAll
    public static void setupConsoleCaptor() {
        consoleCaptor = new ConsoleCaptor();
    }

    @AfterEach
    public void clearOutput() {
        consoleCaptor.clearOutput();
    }

    @AfterAll
    public static void tearDown() {
        consoleCaptor.close();
    }

Do you have any idea what I might be missing or doing wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.