package com.twu.biblioteca;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;


public class OutputHandlerTest {
    private ByteArrayOutputStream byteArrayOutputStream;

    @Before
    public void setUp() {
        byteArrayOutputStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(byteArrayOutputStream));
    }

    @Test
    public void shouldBeAbleToDisplayWelcomeMessage() {
        OutputHandler outputHandler = new OutputHandler();
        outputHandler.welcomeMessage();

        String actualMessage = byteArrayOutputStream.toString();

        assertThat(actualMessage, is(equalTo("Welcome to Biblioteca\n")));
    }

    @After
    public void tearDown() throws Exception {
        System.setOut(null);
    }
}
package com.twu.biblioteca;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;


public class OutputHandlerTest {
    private ByteArrayOutputStream byteArrayOutputStream;
    private OutputHandler outputHandler;

    @Before
    public void setUp() {
        byteArrayOutputStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(byteArrayOutputStream));
        outputHandler = new OutputHandler();
    }

    @Test
    public void shouldBeAbleToDisplayWelcomeMessage() {
        outputHandler.welcomeMessage();

        String actualMessage = byteArrayOutputStream.toString();

        assertThat(actualMessage, is(equalTo("Welcome to Biblioteca\n")));
    }

    @After
    public void tearDown() throws Exception {
        System.setOut(null);
    }
}
Refactor - moved object instatiation in to setUp in OutputHandlerTest