I am writing unit tests for legacy code that uses a logger similar to this:
public class MyLogger
{
private static Logger logger;
public static void init() throws SomeException {
//initialize the logger field
}
public static Logger getLogger() {
return logger;
}
}
The logger requires initialization, otherwise it returns null, and the tests fail because of a NullPointerException coming from the code under test that uses the logger.
My goal is to test the legacy code without depending on the logger at all (initialized or not) when i test.
Possible solutions:
- Add a call to MyLogger.init() in the test - bad solution because of many reasons.
- Initialize the logger in getLogger if it hasn't been initialized - i don't like this solution because then getLogger has to deal with 'SomeException'.
- Initialize the logger with a "NullLogger" that does nothing.
I chose solution #3, but that requires me to inherit from Logger, which has about 60 functions, and call its constructor, and carefully check which methods of the logger are called from my code, so i can choose a proper "null" implementation.
Is there a better solution to my problem?
Aucun commentaire:
Enregistrer un commentaire