{"id":2131,"date":"2012-10-01T01:00:00","date_gmt":"2012-10-01T01:00:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits-expectedexception-and-rule.html"},"modified":"2012-10-22T17:28:08","modified_gmt":"2012-10-22T17:28:08","slug":"testing-custom-exceptions-with-junits","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html","title":{"rendered":"Testing Custom Exceptions with JUnit&#8217;s ExpectedException and @Rule"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\"><strong>Exception Testing<\/strong><\/p>\n<p>Why test exception flows? Just like with all of your code, test coverage writes a contract between your code and the business functionality that the code is supposed to produce leaving you with a <strong>living documentation<\/strong> of the code along with the added ability to stress the functionality early and often. I won&#8217;t go into the many benefits of testing instead I will focus on just Exception Testing.                <\/p>\n<p>There are many ways to test an exception flow thrown from a piece of code. Lets say that you have a guarded method that requires an argument to be not null. How would you test that condition? How do you keep JUnit from reporting a failure when the exception is thrown? This blog covers a few different methods culminating with <strong>JUnit&#8217;s ExpectedException<\/strong> implemented with <strong>JUnit&#8217;s @Rule functionality<\/strong>.<br \/>\n<strong><br \/>\n<\/strong><strong>The &#8216;old&#8217; way<\/strong>              <\/p>\n<p>In a not so distant past the process to test an exception required a dense amount of boilerplate code in which you would start a try\/catch block, report a failure if your code did not produce the expected behavior and then catch the exception looking for the specific type. Here is an example:                <\/p>\n<pre class=\"brush:java\">public class MyObjTest {\r\n\r\n    @Test\r\n    public void getNameWithNullValue() {\r\n\r\n        try {\r\n            MyObj obj = new MyObj();\r\n            myObj.setName(null);\r\n            \r\n            fail('This should have thrown an exception');\r\n\r\n        } catch (IllegalArgumentException e) {\r\n            assertThat(e.getMessage().equals('Name must not be null'));\r\n        }\r\n    }\r\n}<\/pre>\n<p>As you can see from this old example, many of the lines in the test case are just to support the lack of functionality present to specifically test exception handling. One <strong>good point<\/strong> to make for the try\/catch method is the ability to test the <strong>specific message and any custom fields<\/strong> on the expected exception. We will explore this a bit further down with JUnit&#8217;s ExpectedException and @Rule annotation.                <\/p>\n<p><strong>JUnit adds expected exceptions<\/strong>              <\/p>\n<p>JUnit responded back to the users need for exception handling by adding a @Test annotation field &#8216;expected&#8217;. The intention is that the entire test case will pass if the type of exception thrown matched the exception class present in the annotation.                <\/p>\n<pre class=\"brush:java\">public class MyObjTest {\r\n\r\n    @Test(expected = IllegalArgumentException.class)\r\n    public void getNameWithNullValue() {\r\n        MyObj obj = new MyObj();\r\n        myObj.setName(null);\r\n    }\r\n}<\/pre>\n<p>As you can see from the newer example, there is quite a bit less boiler plate code and the test is very concise, however, <strong>there are a few flaws<\/strong>. The main flaw is that the test condition is too broad. Suppose you have two variables in a signature and both cannot be null, then how do you know which variable the IllegalArgumentException was thrown for? What happens when you have extended a Throwable and need to check for the presence of a field? Keep these in mind as you read further, solutions will follow.                <div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><strong>JUnit @Rule and ExpectedException<\/strong>              <\/p>\n<p>If you look at the previous example you might see that you are expecting an IllegalArgumentException to be thrown, but what if you have a custom exception? What if you want to make sure that the message contains a specific error code or message? This is where JUnit really excelled by providing a JUnit @Rule object specifically tailored to exception testing. If you are unfamiliar with JUnit @Rule, read the <a href=\"http:\/\/kentbeck.github.com\/junit\/javadoc\/4.10\/org\/junit\/Rule.html\" target=\"_blank\">docs here<\/a>.                <\/p>\n<p><strong>ExpectedException<\/strong>              <\/p>\n<p>JUnit provides a JUnit class ExpectedException intended to be used as a @Rule. The ExpectedException allows for your test to declare that an exception is expected and gives you some basic built in functionality to clearly express the expected behavior. Unlike the @Test(expected) annotation feature, ExpectedException class allows you to test for specific error messages and custom fields via the <a href=\"http:\/\/code.google.com\/p\/hamcrest\/wiki\/MatcherLibraries\" target=\"_blank\">Hamcrest matchers library<\/a>.                <\/p>\n<p>An example of JUnit&#8217;s ExpectedException               <\/p>\n<pre class=\"brush:java\">import org.junit.rules.ExpectedException;\r\n\r\npublic class MyObjTest {\r\n\r\n    @Rule\r\n    public ExpectedException thrown = ExpectedException.none();\r\n\r\n    @Test\r\n    public void getNameWithNullValue() {\r\n        thrown.expect(IllegalArgumentException.class);\r\n        thrown.expectMessage('Name must not be null');\r\n\r\n        MyObj obj = new MyObj();\r\n        obj.setName(null);\r\n    }\r\n}<\/pre>\n<p>As I eluded to above, the framework allows you to test for specific messages ensuring that the exception being thrown is the case that the test is specifically looking for. This is very helpful when the nullability of multiple arguments is in question.                <\/p>\n<p><strong>Custom Fields<\/strong>              <\/p>\n<p>Arguably the <strong>most useful feature<\/strong> of the ExpectedException framework is the ability to use <strong>Hamcrest matchers<\/strong> to test your custom\/extended exceptions. For example, you have a custom\/extended exception that is to be thrown in a method and inside the exception has an &#8216;errorCode&#8217;. How do you test that functionality without introducing the boiler plate code from the try\/catch block listed above? How about a custom Matcher!                <\/p>\n<p><strong>This code is available at: <a href=\"https:\/\/github.com\/mike-ensor\/custom-exception-testing\" target=\"_blank\">https:\/\/github.com\/mike-ensor\/custom-exception-testing<\/a><\/strong>              <\/p>\n<p><strong>Solution: First the test case<\/strong>              <\/p>\n<pre class=\"brush:java\">import org.junit.rules.ExpectedException;\r\n\r\npublic class MyObjTest {\r\n\r\n    @Rule\r\n    public ExpectedException thrown = ExpectedException.none();\r\n\r\n    @Test\r\n    public void someMethodThatThrowsCustomException() {\r\n        thrown.expect(CustomException.class);\r\n        thrown.expect(CustomMatcher.hasCode('110501'));\r\n\r\n        MyObj obj = new MyObj();\r\n        obj.methodThatThrowsCustomException();\r\n    }\r\n}<\/pre>\n<p><strong>Solution: Custom matcher<\/strong>              <\/p>\n<pre class=\"brush:java\">import com.thepixlounge.exceptions.CustomException;\r\nimport org.hamcrest.Description;\r\nimport org.hamcrest.TypeSafeMatcher;\r\n\r\npublic class CustomMatcher extends TypeSafeMatcher&lt;CustomException&gt; {\r\n\r\n    public static BusinessMatcher hasCode(String item) {\r\n        return new BusinessMatcher(item);\r\n    }\r\n\r\n    private String foundErrorCode;\r\n    private final String expectedErrorCode;\r\n\r\n    private CustomMatcher(String expectedErrorCode) {\r\n        this.expectedErrorCode = expectedErrorCode;\r\n    }\r\n\r\n    @Override\r\n    protected boolean matchesSafely(final CustomException exception) {\r\n        foundErrorCode = exception.getErrorCode();\r\n        return foundErrorCode.equalsIgnoreCase(expectedErrorCode);\r\n    }\r\n\r\n    @Override\r\n    public void describeTo(Description description) {\r\n        description.appendValue(foundErrorCode)\r\n                .appendText(' was not found instead of ')\r\n                .appendValue(expectedErrorCode);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>NOTE: Please visit <a href=\"https:\/\/github.com\/mike-ensor\/custom-exception-testing\" target=\"_blank\">https:\/\/github.com\/mike-ensor\/custom-exception-testing<\/a> to get a copy of a working Hamcrest Matcher, JUnit @Rule and ExpectedException.<\/strong>              <\/p>\n<p>And there you have it, a quick overview of different ways to test Exceptions thrown by your code along with the ability to test for specific messages and fields from within custom exception classes. Please be specific with your test cases and try to target the exact case you have setup for your test, remember, tests can save you from introducing side-effect bugs!<\/p>\n<p>Happy coding and don&#8217;t forget to share!<\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/www.ensor.cc\/2012\/09\/testing-custom-exceptions-w-junits.html\">Testing Custom Exceptions w\/ JUnit&#8217;s ExpectedException and @Rule<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Mike at the <a href=\"http:\/\/www.ensor.cc\/\">Mike&#8217;s site<\/a> blog.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Exception Testing Why test exception flows? Just like with all of your code, test coverage writes a contract between your code and the business functionality that the code is supposed to produce leaving you with a living documentation of the code along with the added ability to stress the functionality early and often. I won&#8217;t &hellip;<\/p>\n","protected":false},"author":290,"featured_media":176,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[274,273],"class_list":["post-2131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-junit","tag-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Testing Custom Exceptions with JUnit&#039;s ExpectedException and @Rule - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Exception Testing Why test exception flows? Just like with all of your code, test coverage writes a contract between your code and the business\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Testing Custom Exceptions with JUnit&#039;s ExpectedException and @Rule - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Exception Testing Why test exception flows? Just like with all of your code, test coverage writes a contract between your code and the business\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2012-10-01T01:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-22T17:28:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Mike Ensor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Ensor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html\"},\"author\":{\"name\":\"Mike Ensor\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/d6101159899ace00317f98787a7378df\"},\"headline\":\"Testing Custom Exceptions with JUnit&#8217;s ExpectedException and @Rule\",\"datePublished\":\"2012-10-01T01:00:00+00:00\",\"dateModified\":\"2012-10-22T17:28:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html\"},\"wordCount\":808,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"keywords\":[\"JUnit\",\"Testing\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html\",\"name\":\"Testing Custom Exceptions with JUnit's ExpectedException and @Rule - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"datePublished\":\"2012-10-01T01:00:00+00:00\",\"dateModified\":\"2012-10-22T17:28:08+00:00\",\"description\":\"Exception Testing Why test exception flows? Just like with all of your code, test coverage writes a contract between your code and the business\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/10\\\/testing-custom-exceptions-with-junits.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Testing Custom Exceptions with JUnit&#8217;s ExpectedException and @Rule\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/d6101159899ace00317f98787a7378df\",\"name\":\"Mike Ensor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5f747c7b2f4a7442b33f2d174e0cb5bded763716c423eef5e22880e85393d6c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5f747c7b2f4a7442b33f2d174e0cb5bded763716c423eef5e22880e85393d6c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5f747c7b2f4a7442b33f2d174e0cb5bded763716c423eef5e22880e85393d6c?s=96&d=mm&r=g\",\"caption\":\"Mike Ensor\"},\"sameAs\":[\"http:\\\/\\\/www.ensor.cc\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/mike-ensor\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Testing Custom Exceptions with JUnit's ExpectedException and @Rule - Java Code Geeks","description":"Exception Testing Why test exception flows? Just like with all of your code, test coverage writes a contract between your code and the business","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html","og_locale":"en_US","og_type":"article","og_title":"Testing Custom Exceptions with JUnit's ExpectedException and @Rule - Java Code Geeks","og_description":"Exception Testing Why test exception flows? Just like with all of your code, test coverage writes a contract between your code and the business","og_url":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2012-10-01T01:00:00+00:00","article_modified_time":"2012-10-22T17:28:08+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","type":"image\/jpeg"}],"author":"Mike Ensor","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Mike Ensor","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html"},"author":{"name":"Mike Ensor","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/d6101159899ace00317f98787a7378df"},"headline":"Testing Custom Exceptions with JUnit&#8217;s ExpectedException and @Rule","datePublished":"2012-10-01T01:00:00+00:00","dateModified":"2012-10-22T17:28:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html"},"wordCount":808,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","keywords":["JUnit","Testing"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html","url":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html","name":"Testing Custom Exceptions with JUnit's ExpectedException and @Rule - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","datePublished":"2012-10-01T01:00:00+00:00","dateModified":"2012-10-22T17:28:08+00:00","description":"Exception Testing Why test exception flows? Just like with all of your code, test coverage writes a contract between your code and the business","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2012\/10\/testing-custom-exceptions-with-junits.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Testing Custom Exceptions with JUnit&#8217;s ExpectedException and @Rule"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/d6101159899ace00317f98787a7378df","name":"Mike Ensor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d5f747c7b2f4a7442b33f2d174e0cb5bded763716c423eef5e22880e85393d6c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d5f747c7b2f4a7442b33f2d174e0cb5bded763716c423eef5e22880e85393d6c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5f747c7b2f4a7442b33f2d174e0cb5bded763716c423eef5e22880e85393d6c?s=96&d=mm&r=g","caption":"Mike Ensor"},"sameAs":["http:\/\/www.ensor.cc\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/mike-ensor"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/2131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/290"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=2131"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/2131\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/176"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=2131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=2131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=2131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}