Last active
December 26, 2015 04:08
-
-
Save eleco/7090485 to your computer and use it in GitHub Desktop.
hide exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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