Skip to content

Instantly share code, notes, and snippets.

@eleco
Last active December 26, 2015 04:08
Show Gist options
  • Save eleco/7090485 to your computer and use it in GitHub Desktop.
Save eleco/7090485 to your computer and use it in GitHub Desktop.
hide exception
package com.company;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Item {
Logger log = Logger.getLogger("ItemLogger");
public boolean function(String param) {
if (param==null) {
log.log(Level.SEVERE,"error", new IllegalArgumentException("boom"));
return false;
}
return true;
}
}
package com.company;
import org.junit.Rule;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class ItemTest {
@Rule
public org.junit.rules.TestRule watcher = new TestWatcherLoggingRule("testFunction","ItemLogger");
@Test
public void testFunction() {
assertThat(new Item().function(null),is(false));
}
}
package com.company;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TestWatcherLoggingRule extends TestWatcher {
//name of the method to suspend logging for
private final String methodName;
private Level loggingLevel;
private Logger logger;
public TestWatcherLoggingRule(String methodName, String loggerName) {
this.methodName = methodName;
this.logger = Logger.getLogger(loggerName);
this.loggingLevel = logger.getLevel();
}
@Override
public void starting(Description desc) {
if (desc.getMethodName().equals(methodName))
logger.setLevel(Level.OFF);
}
@Override
public void finished(Description desc) {
logger.setLevel(loggingLevel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment