Introduction
Testing can sometimes make your head spin. For example when a method is a void and you are not allowed to use powermock.
The thing with a method like that, is that it is not returning an Object on which you can test on.
If you are lucky it might have a parameter that changes during the execution of the method.
Sometimes it has a log statement based on a Exception or on an event captured in an if statement, that happened during execution.
That gives also opportunities. Let me show how.
The solution
For working with log files the best thing to do is approach them with log appenders.
//We start with a mock appender:
@Mockprivate Appender mockAppender;//Creating a rootlogger to hold all the appenders.
ch.qos.logback.classic.Logger root =(ch.qos.logback.classic.Logger) LoggerFactory.getLogger (ch.qos.logback.classic.Logger.ROO T_LOGGER_NAME); // appending the ArgumentMatcher to the mockAppender.Mockito.verify(mockAppender).doAppend(Mockito.argThat (new ArgumentMatcher<Object>() {//implementing the matches method to see if the
// log text appears in the log file
@Overridepublic boolean matches(final Object argument) { return ((LoggingEvent) argument).getFormattedMessage() //The text we want to search.
.contains("test logging text that should be found");}}));
The conclusion
Void methods could be still a call for an asperine when it comes to testing. There are some helpful methods I believe that this is one of them. I used it this time to test a method that checked if an certificate was expired. This way I could pump up the test coverage of my code.
Have fun!
Geen opmerkingen:
Een reactie posten