Created
September 23, 2012 20:05
-
-
Save jarek-przygodzki/3772891 to your computer and use it in GitHub Desktop.
Unit test for stackless exceptions
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
import org.junit.Test; | |
import static org.junit.Assert.assertThat; | |
import org.hamcrest.BaseMatcher; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
/** | |
* Unit test for stackless exceptions | |
*/ | |
public class StacklessExceptionsTest { | |
@Test | |
public void testCheckedException() { | |
try { | |
throw new StacklessException(); | |
} catch (StacklessException e) { | |
assertThat(e, hasNoStackTrace()); | |
} | |
} | |
@Test | |
public void testUncheckedException() { | |
try { | |
throw new RuntimeStacklessException(); | |
} catch (RuntimeStacklessException e) { | |
assertThat(e, hasNoStackTrace()); | |
} | |
} | |
private static class ThrowableHasNoStackTrace extends | |
BaseMatcher<Throwable> { | |
@Override | |
public boolean matches(Object object) { | |
Throwable t = (Throwable) object; | |
return t.getStackTrace().length == 0; | |
} | |
@Override | |
public void describeTo(Description desc) { | |
desc.appendText("stackless exception has a stack trace"); | |
} | |
} | |
static Matcher<? super Throwable> hasNoStackTrace() { | |
return new ThrowableHasNoStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment