mardi 23 août 2016

Testing console output

Got a question regarding testing the console output.

stdOutput class:

public abstract class StdOutTest {

    private final PrintStream stdOutMock = mock(PrintStream.class);
    private final PrintStream stdOutOrig = System.out;

    @Before
    public void setUp() {
        System.setOut(this.stdOutMock);
    }

    @After
    public void tearDown() {
        System.setOut(this.stdOutOrig);
    }

    protected final PrintStream getStdOutMock() {
        return this.stdOutMock;
    }
}

Now here is something I don't understand:

public class test extends StdOutTest{

    @Before
    public void setUp(){
        //empty
    }

    @Test
    public void example(){
        System.out.println("hello");
        verify(getStdOutMock()).println("hello");
    }
}

I use Mockito for the verify and this test passes when I delete setUp(), but with the setUp() it fails. the fail message says:

hello


Wanted but not invoked:
printStream.println("hello");
-> at observer_test.test.example(test.java:18)
Actually, there were zero interactions with this mock.

Can anyone help me perhaps on why this happens?

Aucun commentaire:

Enregistrer un commentaire