{"id":2837,"date":"2012-11-03T19:13:34","date_gmt":"2012-11-03T17:13:34","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=2837"},"modified":"2012-12-04T11:43:39","modified_gmt":"2012-12-04T09:43:39","slug":"junit-rules","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html","title":{"rendered":"JUnit Rules"},"content":{"rendered":"<p>The first time I stumbled over a <a href=\"http:\/\/junit.org\" target=\"_blank\">JUnit<\/a> <code><a href=\"http:\/\/kentbeck.github.com\/junit\/javadoc\/4.10\/org\/junit\/Rule.html\" target=\"_blank\">@Rule<\/a><\/code> annotation I was a bit irritated of the concept. Having a public field in a test case seemed somewhat odd and so I was reluctant to use it regularly. But after a while I got used to that and it turned out that rules can ease writing tests in many ways. This post gives a quick introduction of the concept and some short examples of what rules are good for.<\/p>\n<p><strong>What are JUnit Rules?<\/strong><\/p>\n<p>Let\u2019s start with a look at a JUnit out-of-the-box rule. The <code><a href=\"http:\/\/kentbeck.github.com\/junit\/javadoc\/4.10\/org\/junit\/rules\/TemporaryFolder.html\" target=\"blank\">TemporaryFolder<\/a><\/code> is a test helper that can be used to create files and folders located under the file system directory for temporary content<sup><a href=\"#fn-1945-1\">1<\/a><\/sup>. The interesting thing with the <code>TemporaryFolder<\/code> is that it guarantees to delete its files and folders when the test method finishes<sup><a href=\"#fn-1945-2\">2<\/a><\/sup>. To work as expected the temporary folder instance must be assigned to an <code>@Rule<\/code> annotated field that must be public, not static, and a subtype of <code><a href=\"http:\/\/kentbeck.github.com\/junit\/javadoc\/4.10\/org\/junit\/rules\/TestRule.html\" target=\"_blank\">TestRule<\/a><\/code>:<\/p>\n<pre class=\" brush:java\">public class MyTest {\r\n\r\n  @Rule\r\n  public TemporaryFolder temporaryFolder = new TemporaryFolder();\r\n\r\n  @Test\r\n  public void testRun() throws IOException {\r\n    assertTrue( temporaryFolder.newFolder().exists() );\r\n  }\r\n}<\/pre>\n<p>&nbsp;<br \/>\n<strong>How does it work?<\/strong><\/p>\n<p>Rules provide a possibility to intercept test method calls similar as an <a href=\"http:\/\/en.wikipedia.org\/wiki\/Aspect-oriented_programming\" target=\"_blank\">AOP<\/a> framework would do. Comparable to an around advice in <a href=\"http:\/\/www.eclipse.org\/aspectj\/\" target=\"_blank\">AspectJ<\/a> you can do useful things before and\/or after the actual test execution<sup><a href=\"#fn-1945-3\">3<\/a><\/sup>. Although this sounds complicated it is quite easy to achieve.<\/p>\n<p>The API part of a rule definition has to implement TestRule. The only method of this interface called <code>apply<\/code> returns a <code><a href=\"http:\/\/kentbeck.github.com\/junit\/javadoc\/4.10\/org\/junit\/runners\/model\/Statement.html\" target=\"_blank\">Statement<\/a><\/code>. <code>Statement<\/code>s represent \u2013 simply spoken \u2013 your tests within the JUnit runtime and <code>Statement#evaluate()<\/code> executes them. Now the basic idea is to provide wrapper extensions of <code>Statement<\/code> that can do the actual contributions by overriding <code>Statement#evaluate()<\/code>:<\/p>\n<pre class=\" brush:java\">public class MyRule implements TestRule {\r\n\r\n  @Override\r\n  public Statement apply( Statement base, Description description ) {\r\n    return new MyStatement( base );\r\n  }\r\n}\r\n\r\npublic class MyStatement extends Statement {\r\n\r\n  private final Statement base;\r\n\r\n  public MyStatement( Statement base ) {\r\n    this.base = base;\r\n  }\r\n\r\n  @Override\r\n  public void evaluate() throws Throwable {\r\n    System.out.println( 'before' );\r\n    try {\r\n      base.evaluate();\r\n    } finally {\r\n      System.out.println( 'after' );\r\n    }\r\n  }\r\n}<\/pre>\n<p><code>MyStatement<\/code> is implemented as wrapper that is used in <code>MyRule#apply(Statement,Destination)<\/code> to wrap the original statement given as argument. It is easy to see that the wrapper overrides <code>Statement#evaluate()<\/code> to do something before and after the actual evaluation of the test<sup><a href=\"#fn-1945-4\">4<\/a><\/sup>.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>The next snippet shows how <code>MyRule<\/code> can be used exactly the same way as the <code>TemporaryFolder<\/code> above:<\/p>\n<pre class=\" brush:java\">public class MyTest {\r\n\r\n  @Rule\r\n  public MyRule myRule = new MyRule();\r\n\r\n  @Test\r\n  public void testRun() {\r\n    System.out.println( 'during' );\r\n  }\r\n}<\/pre>\n<p>Launching the test case leads to the following console output which proves that our example rule works as expected. The test execution gets intercepted and modified by our rule to print \u2018before\u2019 and \u2018after\u2019 around the \u2018during\u2019 of the test:<\/p>\n<pre class=\" brush:java\">before\r\nduring\r\nafter<\/pre>\n<p>Now that the basics are understood let\u2019s have a look at slightly more useful things you could do with rules.<\/p>\n<p><strong>Test Fixtures<\/strong><\/p>\n<p>Quoted from the according <a href=\"http:\/\/en.wikipedia.org\/wiki\/Test_fixture#Test_fixture_in_xUnit\" target=\"_blank\">wikipedia section<\/a> a test fixture \u2018is all the things that must be in place in order to run a test and expect a particular outcome. Frequently fixtures are created by handling <code>setUp()<\/code> and <code>tearDown()<\/code> events of the unit testing framework\u2019.<\/p>\n<p>With JUnit this often looks somewhat like this:<\/p>\n<pre class=\" brush:java\">public class MyTest {\r\n\r\n  private MyFixture myFixture;\r\n\r\n  @Test\r\n  public void testRun1() {\r\n    myFixture.configure1();\r\n    \/\/ do some testing here\r\n  }\r\n\r\n  @Test\r\n  public void testRun2() {\r\n    myFixture.configure2();\r\n    \/\/ do some testing here\r\n  }\r\n\r\n  @Before\r\n  public void setUp() {\r\n    myFixture = new MyFixture();\r\n  }\r\n\r\n  @After\r\n  public void tearDown() {\r\n    myFixture.dispose();\r\n  }\r\n}<\/pre>\n<p>Consider you use a particular fixture the way shown above in many of your tests. In that case it could be nice to get rid of the <code>setUp()<\/code> and <code>tearDown()<\/code> methods. Given the sections above we now know that this can be done by changing <code>MyFixture<\/code> to implement <code>TestRule<\/code>. An appropriate <code>Statement<\/code> implementation would have to ensure that it calls <code>MyFixture#dispose()<\/code> and could look like this:<\/p>\n<pre class=\" brush:java\">public class MyFixtureStatement extends Statement {\r\n\r\n  private final Statement base;\r\n  private final MyFixture fixture;\r\n\r\n  public MyFixtureStatement( Statement base, MyFixture fixture ) {\r\n    this.base = base;\r\n    this.fixture = fixture;\r\n  }\r\n\r\n  @Override\r\n  public void evaluate() throws Throwable {\r\n    try {\r\n      base.evaluate();\r\n    } finally {\r\n      fixture.dispose();\r\n    }\r\n  }\r\n}<\/pre>\n<p>With this in place the test above can be rewritten as:<\/p>\n<pre class=\" brush:java\">public class MyTest {\r\n\r\n  @Rule\r\n  public MyFixture myFixture = new MyFixture();\r\n\r\n  @Test\r\n  public void testRun1() {\r\n    myFixture.configure1();\r\n    \/\/ do some testing here\r\n  }\r\n\r\n  @Test\r\n  public void testRun2() {\r\n    myFixture.configure2();\r\n    \/\/ do some testing here\r\n  }\r\n}<\/pre>\n<p>I come to appreciate the more compact form of writing tests using rules in a lot of cases, but surely this is also a question of taste and what you consider better to read<sup><a href=\"#fn-1945-5\">5<\/a><\/sup>.<\/p>\n<p><strong>Fixture Configuration with Method Annotations<\/strong><\/p>\n<p>So far I have silently ignored the <code>Description<\/code> argument of <code>TestRule#apply(Statement,Description)<\/code>. In general a <code>Description<\/code> describes a test which is about to run or has been run. But it also allows access to some reflective information about the underlying java method. Among others it is possible to read the annotations attached to such a method. This enables us to combine rules with method annotations for convenience configuration of a <code>TestRule<\/code>.<\/p>\n<p>Consider this annotation type:<\/p>\n<pre class=\" brush:java\">@Retention(RetentionPolicy.RUNTIME)\r\n@Target({ElementType.METHOD})\r\npublic @interface Configuration {\r\n  String value();\r\n}<\/pre>\n<p>Combined with the following snippet inside <code>MyFixture#apply(Statement,Destination)<\/code> that reads the configuration value annotated to a certain test method\u2026<\/p>\n<pre class=\" brush:java\">Configuration annotation\r\n  = description.getAnnotation( Configuration.class );\r\nString value = annotation.value();\r\n\/\/ do something useful with value<\/pre>\n<p>\u2026 the test case above that demonstrates the usage of the <code>MyFixture<\/code> rule can be rewritten to:<\/p>\n<pre class=\" brush:java\">public class MyTest {\r\n\r\n  @Rule\r\n  public MyFixture myFixture = new MyFixture();\r\n\r\n  @Test\r\n  @Configuration( value = 'configuration1' )\r\n  public void testRun1() {\r\n    \/\/ do some testing here\r\n  }\r\n\r\n  @Test\r\n  @Configuration( value = 'configuration2' )\r\n  public void testRun2() {\r\n    \/\/ do some testing here\r\n  }\r\n}<\/pre>\n<p>Of course there are limitations to the latter approach due to the fact that annotations only allow <code>Enum<\/code>s, <code>Class<\/code>es or <code>String<\/code> literals as parameters. But there are use cases where this is completely sufficient. A nice example using rules combined with method annotations is provided by the <a href=\"http:\/\/www.restfuse.com\" target=\"_blank\">restfuse<\/a> library. If you are interested in a real world example you should have a look at the library\u2019s implementation of the <code><a href=\"https:\/\/github.com\/eclipsesource\/restfuse\/blob\/master\/com.eclipsesource.restfuse\/src\/com\/eclipsesource\/restfuse\/Destination.java\" target=\"_blank\">Destination<\/a> <\/code>rule<sup><a href=\"#fn-1945-6\">6<\/a><\/sup>.<\/p>\n<p>Comming to the end the only thing left to say is that I would love to hear from you about other useful examples of JUnit rules you might use to ease your daily testing work:<\/p>\n<ol>\n<li>The directory which is in general returned by <code>System.getProperty( 'java.io.tmpdir' );<\/code> <a href=\"#fnref-1945-1\">\u21a9<\/a><\/li>\n<li>Looking at the implementation of <code>TemporaryFolder<\/code> I must note that it does not check if the deletion of a file is successful. This might be a weak point in case of open file handles <a href=\"#fnref-1945-2\">\u21a9<\/a><\/li>\n<li>And for what it\u2019s worth you even could replace the complete test method by something else <a href=\"#fnref-1945-3\">\u21a9<\/a><\/li>\n<li>The delegation to the wrapped statement is put into a <code>try...finally<\/code> block to ensure that the functionality after the test gets executed, even if the test would fail. In that case an <code>AssertionError<\/code> would be thrown and all statements that are not in the finally block would be skipped <a href=\"#fnref-1945-4\">\u21a9<\/a><\/li>\n<li>You probably noted that the <code>TemporaryFolder<\/code> example at the beginning is also nothing else but a fixture use case <a href=\"#fnref-1945-5\">\u21a9<\/a><\/li>\n<li>Note that restfuse\u2019s <code>Destination<\/code> class implements <code>MethodRule<\/code> instead of <code>TestRule<\/code>. This post is based on the latest JUnit version where <code>MethodRule<\/code> has been marked as <code>@Deprecated<\/code>. <code>TestRule<\/code> is the replacement for <code>MethodRule<\/code>. But given the knowledge of this post it should be nevertheless easily possible to understand the implementation <a href=\"#fnref-1945-6\">\u21a9<\/a><\/li>\n<\/ol>\n<p>&nbsp;<br \/>\n<strong><em>Reference: <\/em><\/strong><a href=\"http:\/\/www.codeaffine.com\/2012\/09\/24\/junit-rules\/\">JUnit Rules<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Frank Appel at the <a href=\"http:\/\/www.codeaffine.com\/\">Code Affine<\/a> blog.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first time I stumbled over a JUnit @Rule annotation I was a bit irritated of the concept. Having a public field in a test case seemed somewhat odd and so I was reluctant to use it regularly. But after a while I got used to that and it turned out that rules can ease &hellip;<\/p>\n","protected":false},"author":336,"featured_media":176,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[274],"class_list":["post-2837","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-junit"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JUnit Rules<\/title>\n<meta name=\"description\" content=\"The first time I stumbled over a JUnit @Rule annotation I was a bit irritated of the concept. Having a public field in a test case seemed somewhat odd and\" \/>\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\/11\/junit-rules.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JUnit Rules\" \/>\n<meta property=\"og:description\" content=\"The first time I stumbled over a JUnit @Rule annotation I was a bit irritated of the concept. Having a public field in a test case seemed somewhat odd and\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.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-11-03T17:13:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-12-04T09:43:39+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=\"Frank Appel\" \/>\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=\"Frank Appel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html\"},\"author\":{\"name\":\"Frank Appel\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ff5a13a51eb4394c29d6442242d3689c\"},\"headline\":\"JUnit Rules\",\"datePublished\":\"2012-11-03T17:13:34+00:00\",\"dateModified\":\"2012-12-04T09:43:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html\"},\"wordCount\":928,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"keywords\":[\"JUnit\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html\",\"name\":\"JUnit Rules\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"datePublished\":\"2012-11-03T17:13:34+00:00\",\"dateModified\":\"2012-12-04T09:43:39+00:00\",\"description\":\"The first time I stumbled over a JUnit @Rule annotation I was a bit irritated of the concept. Having a public field in a test case seemed somewhat odd and\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/11\\\/junit-rules.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\\\/11\\\/junit-rules.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\":\"JUnit Rules\"}]},{\"@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\\\/ff5a13a51eb4394c29d6442242d3689c\",\"name\":\"Frank Appel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9ea88be0ddf77d1d9ca0bd16528b136e6d56f2b6f6577c93cae14f3e683cf57a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9ea88be0ddf77d1d9ca0bd16528b136e6d56f2b6f6577c93cae14f3e683cf57a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9ea88be0ddf77d1d9ca0bd16528b136e6d56f2b6f6577c93cae14f3e683cf57a?s=96&d=mm&r=g\",\"caption\":\"Frank Appel\"},\"sameAs\":[\"http:\\\/\\\/www.codeaffine.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/frank-appel\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JUnit Rules","description":"The first time I stumbled over a JUnit @Rule annotation I was a bit irritated of the concept. Having a public field in a test case seemed somewhat odd and","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\/11\/junit-rules.html","og_locale":"en_US","og_type":"article","og_title":"JUnit Rules","og_description":"The first time I stumbled over a JUnit @Rule annotation I was a bit irritated of the concept. Having a public field in a test case seemed somewhat odd and","og_url":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2012-11-03T17:13:34+00:00","article_modified_time":"2012-12-04T09:43:39+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":"Frank Appel","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Frank Appel","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html"},"author":{"name":"Frank Appel","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ff5a13a51eb4394c29d6442242d3689c"},"headline":"JUnit Rules","datePublished":"2012-11-03T17:13:34+00:00","dateModified":"2012-12-04T09:43:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html"},"wordCount":928,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","keywords":["JUnit"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html","url":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html","name":"JUnit Rules","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","datePublished":"2012-11-03T17:13:34+00:00","dateModified":"2012-12-04T09:43:39+00:00","description":"The first time I stumbled over a JUnit @Rule annotation I was a bit irritated of the concept. Having a public field in a test case seemed somewhat odd and","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2012\/11\/junit-rules.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\/11\/junit-rules.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":"JUnit Rules"}]},{"@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\/ff5a13a51eb4394c29d6442242d3689c","name":"Frank Appel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9ea88be0ddf77d1d9ca0bd16528b136e6d56f2b6f6577c93cae14f3e683cf57a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/9ea88be0ddf77d1d9ca0bd16528b136e6d56f2b6f6577c93cae14f3e683cf57a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9ea88be0ddf77d1d9ca0bd16528b136e6d56f2b6f6577c93cae14f3e683cf57a?s=96&d=mm&r=g","caption":"Frank Appel"},"sameAs":["http:\/\/www.codeaffine.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/frank-appel"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/2837","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\/336"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=2837"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/2837\/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=2837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=2837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=2837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}