I have a project where I need to provide SLF4J logging if SLF4J is on the classpath and otherwise provide logging straight to the console. I instantiate my logger with code similar to:
try {
Class.forName("org.slf4j.LoggerFactory");
return new Slf4JLogProvider();
} catch (ClassNotFoundException e) {
System.out.println("JmxTrans: SLF4J provider not on classpath, defaulting to console logging");
}
return new ConsoleLogProvider();
My project is Maven based and this module declares an optional dependency on SLF4J:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<optional>true</optional>
</dependency>
I would like to be able to unit test this code. Classpath is tricky and properly testing it is IMHO important. Basically, I would like to be able to modify the classloader during my test to ensure SLF4J is absent and validate that console logging is initialized.
Is there any "clean" way of doing this? Any framework that would provide support for classpath dependant tests? Any standard way of isolating unit test in a specific classloader?
Aucun commentaire:
Enregistrer un commentaire