jeudi 30 juin 2016

Testing that a Logback Log statment was called using Junit?

Below is a method that I am attempting to write Junit test for:

Method I wish to test:

public void showOutputOfIdentifications(int age) {

    if(age>25){

        LOGGER.info("Over 25");

    }else{

        LOGGER.info("25 or Under");

    }
 }

How can I test to verify that the correct Logback Log statement has been called?

In my tests I have tried to mock the logger and verify that it was called, however this has not worked?

Current Test Attempt:

 @Test
    public void testShowOutputOfIdentifications() throws ParseException{

    int age = 10;

    Logger LOGGER_mock = mock(Logger.class);

    //call method under test
    showOutputOfIdentifications(age);

    verify(LOGGER_mock).info("25 or under");


}

Current failing test output:

Wanted but not invoked:
logger.info(
    "25 or under "
);

Actually, there were zero interactions with this mock.

Aucun commentaire:

Enregistrer un commentaire