{"id":1267,"date":"2014-10-06T06:03:26","date_gmt":"2014-10-06T13:03:26","guid":{"rendered":"http:\/\/10kdev.net\/?p=1267"},"modified":"2014-10-06T06:43:23","modified_gmt":"2014-10-06T13:43:23","slug":"test-coverage-for-a-void-method","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=1267","title":{"rendered":"Test Coverage for a Void Method"},"content":{"rendered":"<p>I ran across some state code that had plugs in it that were operations an action\u00a0listener would look for. \u00a0The code methods didn&#8217;t\u00a0have anything in them, but needed to be there. \u00a0I wanted some simple unit testing coverage on them. \u00a0Here&#8217;s a small technique to cover a void method that doesn&#8217;t do too much.<\/p>\n<p>Here&#8217;s the code to be tested. Notice I made 2 methods for my tests, one throws an error. I&#8217;ve included to ways of testing for errors in the attached repository code.<\/p>\n<pre><code>\r\npublic class TestClassImpl implements TestClass {\r\n   public void methodNoError() {}\r\n   public void methodError() {throw new RuntimeException();}\t\r\n}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>To test methodNoError, we just use a Boolean and flag it if there is an error, and assert that variable. The other two tets are separate ways to check methodError() which actually throws an error.<\/p>\n<pre><code>\r\n@Test\r\npublic void testMethodNoError() {\t\r\n   Boolean testState = true;\t\r\n   try {\t\r\n      testClass.methodNoError();\t\r\n   } catch (Exception e) {\r\n      testState = false;\r\n   }\t\t\r\n   assertTrue(testState);\r\n}\r\n\t\r\n@Test\r\npublic void testMethodError() {\t\r\n   Boolean testState = true;\r\n   try {\t\r\n      testClass.methodError();\t\r\n   } catch (Exception e) {\r\n      testState = false;\r\n   }\t\r\n   assertFalse(testState);\r\n}\r\n\t\r\n@Test(expected = Exception.class)\r\npublic void testMethodErrorExpected() {testClass.methodError();}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>You can try this out with the TestVoid project folder in my Bitbucket; just run &#8220;mvn clean install&#8221; or load it into your IDE.<\/p>\n<p><a href=\"https:\/\/bitbucket.org\/bilmowry\/10kdev\/src\/78443782dabb50c9f429ac90383bc030d329d8e6\/TestVoid\/?at=master\">TestVoid<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I ran across some state code that had plugs in it that were operations an action\u00a0listener would look for. \u00a0The code methods didn&#8217;t\u00a0have anything in them, but needed to be there. \u00a0I wanted some simple unit testing coverage on them. \u00a0Here&#8217;s a small technique to cover a void method that doesn&#8217;t do too much. Here&#8217;s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[69,68],"tags":[67,29,56],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1267"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1267"}],"version-history":[{"count":4,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1267\/revisions"}],"predecessor-version":[{"id":1273,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1267\/revisions\/1273"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1267"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}