Skip to content

Instantly share code, notes, and snippets.

@simonbrandhof
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save simonbrandhof/5f73d97ffe2aa6283e6f to your computer and use it in GitHub Desktop.

Select an option

Save simonbrandhof/5f73d97ffe2aa6283e6f to your computer and use it in GitHub Desktop.
Proposal for log testing
public class MyClass {
// no coupling with SLF4J
private final org.sonar.api.utils.log.Logger logger = org.sonar.api.utils.log.Loggers.get(getClass());
void foo() {
if (logger.isDebugEnabled()) {
logger.debug("foo");
}
}
}
public class MyClassTest {
@Rule
public org.sonar.api.utils.log.LogTester logTester = new org.sonar.api.utils.log.LogTester();
@Test
public void test() {
// enters in Logger#isDebugEnabled()
logTester.enableDebug(true);
new MyClass().foo();
// verify logs
assertThat(logTester.logs()).contains("foo");
assertThat(logTester.debugLogs()).contains("foo");
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment