0

I have the following simple method within my Java Application:

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 this method to ensure it is logging what it should if a value is over/under 25?

For reference: I am currently using the Junit framework with Mockito. My logger is slf4j.

java123999
  • 6,974
  • 36
  • 77
  • 121
  • Look http://www.coderanch.com/t/96469/Testing/junit-asserts-log-Messages maybe this helps – Jens Jun 29 '16 at 08:47

1 Answers1

1

Provided LOGGER is an object, the classic way is to replace it with a mock. Mockito is a well known mock library, but other are around, just pick one. With mockito, it could look like:

LOGGER_mock = mock(LoggerClass.class);
// inject LOGGER_mock into class to test, eventually use reflection to change
//  a private attribute

classToTest.showOutputOfIdentifications(10);
verify(LOGGER_mock).info("25 or Under");
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252