JUnit Tip: Verifying that an Exception with a Particular Message was Thrown
source link: https://blog.jakubholy.net/2011/09/16/junit-tip-verifying-that-an-exception-with-a-particular-message-was-thrown/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
JUnit Tip: Verifying that an Exception with a Particular Message was Thrown
import org.junit.*;
import org.junit.rules.ExpectedException;
public static class HasExpectedException {
@Rule
public ExpectedException thrown= ExpectedException.none();
@Test
public void throwsNothing() {
// no exception expected, none thrown: passes.
}
@Test
public void throwsNullPointerExceptionWithMessage() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("What happened here?");
thrown.expectMessage(allOf(containsString("What"), containsString("here")));
throw new NullPointerException("What happened here?");
}
}
(As you might have noticed, it uses Hamcrest matchers; containsString isn't included directly in junit and thus you'd need junit-dep + hamcrest jars.)
Are you benefitting from my writing? Consider buying me a coffee or supporting my work via GitHub Sponsors. Thank you! You can also book me for a mentoring / pair-programming session via Codementor or (cheaper) email.
Allow me to write to you!
Let's get in touch! I will occasionally send you a short email with a few links to interesting stuff I found and with summaries of my new blog posts. Max 1-2 emails per month. I read and answer to all replies.
Recommend
-
12
Thrown by scroll inertia in Firefox I've discovered a rather annoying quirk in the combined environment of OS X 10.6 (Snow Leopard) and Firefox. It seems that the Mac thinks a "thrown" scroll lasts much longer than you might imag...
-
6
VS 2015 IntelliSense: Exceptions thrown unvisited advertisements I just upgraded to VS 2015 Enterprise Edition and am working with C#. ...
-
4
What happens if a thrown exception is not handled in C++? In the event that an exception is thrown but not handled, this means that a matching handler does not exist for the exception that was thrown. This also means that an
-
9
Activision Blizzard is trying to get the discrimination suit thrown out of court It alleges DFEH lawyers should be disqualified because of a conflict of interest ...
-
1
Comments ...
-
11
MVC MailMessage does not send it, no exception thrown advertisements I am running my site locally, testing the "Contact Us" page...
-
2
Amazon antitrust lawsuit filed by DC attorney general thrown out in court The attorney general’s office is considering other legal avenues ...
-
10
Hodlonaut’s attempt to have Craig Wright’s defamation suit thrown out denied Business 18 May 2022 The English Hig...
-
5
Apple Argues to Get App Store Injunction Thrown Out in Epic v. Apple Appeal
-
4
Remove particular null field before message mapping through XSLT or Gro...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK