Created
September 4, 2016 09:01
-
-
Save su-kun1899/316be9d77db3eeae898042f91002f49a to your computer and use it in GitHub Desktop.
JUnitでの例外テストの書き方 ref: http://qiita.com/su-kun1899/items/5c9f0294a7de1986e542
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
@Test(expected = SampleException.class) | |
public void throwsSampleException() { | |
// do something | |
} |
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
@Test | |
public void throwsSampleException() { | |
try { | |
// do something | |
... | |
fail(); | |
} catch (SampleException expected) { | |
assertThat(expected.getMessage(), equalTo("exception message")); | |
} | |
} | |
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
@Rule | |
public ExpectedException thrown = ExpectedException.none(); | |
@Test | |
public void exceptionRule() { | |
thrown.expect(SampleException.class); | |
thrown.expectMessage("exception message"); | |
// do something | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment