{"id":247,"date":"2013-08-20T06:55:48","date_gmt":"2013-08-20T06:55:48","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=247"},"modified":"2021-02-08T17:13:22","modified_gmt":"2021-02-08T17:13:22","slug":"junit-part1","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/junit-part1\/","title":{"rendered":"JUnit4 @Before, @After, Exception, Performance and Parameterized Test"},"content":{"rendered":"<p>The below examples are specific to Junit4<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!-- https:\/\/mvnrepository.com\/artifact\/junit\/junit --&gt;\r\n&lt;dependency&gt;\r\n    &lt;groupId&gt;junit&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;junit&lt;\/artifactId&gt;\r\n    &lt;version&gt;4.12&lt;\/version&gt;\r\n    &lt;scope&gt;test&lt;\/scope&gt;\r\n&lt;\/dependency&gt;\r\n<\/pre>\n<p><strong>Basic Test case which Fails<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport static org.junit.Assert.*;\r\nimport org.junit.Test;\r\n\r\npublic class Junit1 {\t\r\n @Test\r\n public void basicTestFail()\r\n {\r\n  fail(&quot;The Test Failed&quot;);\r\n }\r\n}\r\n<\/pre>\n<p>There is No <strong>pass method<\/strong> in JUnit like <strong>fail method<\/strong>.<\/p>\n<p>As long as the test doesn&#8217;t throw an exception, it passes, unless your @Test annotation specifies an expected exception. I suppose a pass() could throw a special exception that JUnit always interprets as passing, so as to short circuit the test, but that would go against the usual design of tests (i.e. assume success and only fail if an assertion fails) and, if people got the idea that it was preferable to use pass(), it would significantly slow down a large suite of passing tests (due to the overhead of exception creation). Failing tests should not be the norm, so it&#8217;s not a big deal if they have that overhead.<\/p>\n<p>Tests to verify the correctness of a program&#8217;s behavior.Verifying the correctness of a program&#8217;s behaviour by inspecting the content of output statements.Its looking manually for abnormal output. <\/p>\n<p><strong>Example1<\/strong><\/p>\n<p><strong>StringHelper.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.junit.tutor;\r\n\r\npublic class StringHelper\r\n{\t\r\n\tpublic static void main(String&#x5B;] args)\r\n\t{\r\n\t\tStringHelper objStringHelper = new StringHelper();\r\n\t\tSystem.out.println(objStringHelper.wordATrimmer(&quot;PABBB&quot;));\r\n\t}\r\n\t\r\n\tpublic String wordATrimmer(String pWord)\r\n\t{\r\n  \t   String pTempString = pWord.substring(0, 2);\t\t\t   \r\n           if(pTempString.contains(&quot;A&quot;)) return pWord.replace(&quot;A&quot;, &quot;&quot;);\t\t\r\n \t   pTempString = pWord.substring(2);\t\t\r\n\t   return pWord;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>StringHelperTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.junit.tutor;\r\n\r\nimport static org.junit.Assert.*;\r\nimport org.junit.Test;\r\n\r\npublic class StringHelperTest \r\n{\r\n\t@Test\r\n\tpublic void test() \r\n        {\r\n\t\tStringHelper objStringHelper = new StringHelper();\r\n\t\tString expectedOP            = &quot;MMG&quot;;\r\n\t\tString actOP                 = objStringHelper.wordATrimmer(&quot;AAMMG&quot;);\r\n\t\t\r\n\t\tassertEquals(expectedOP, actOP);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>@Test<\/strong> comes from <strong>org.junit.Test<\/strong> Package<br \/>\n<strong>assertEquals<\/strong> &#8211; helps in performing String, Boolean and Other Comparison.<\/p>\n<p>As you have seen above <strong>StringHelper.java<\/strong> is the actual file that has the function.<strong>StringHelperTest.java<\/strong> is the file in which test of the functionality is to be carried out. This java file is created in a new source folder test. By using the assert equals method we are comparing whether the expected Op and actual OP are the same.<\/p>\n<p><strong>@Test<\/strong> annotation at the top of the test method tells the JVM to run this method<\/p>\n<p><strong>assertTrue(boolean) <\/strong><br \/>\nassertTrue(boolean true) will return true<br \/>\nassertFalse(boolean false) will return true<\/p>\n<p>To make sure the code is behaving the right way we can use asserttrue() and assertfalse() methods.<br \/>\n<strong>StringHelper.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.junit.tutor;\r\n\r\npublic class StringHelper\r\n{\t\r\n\tpublic boolean areFirstTwoandLastTwoStringsSame(String Name)\r\n\t{\r\n\t\tif(Name.length() &lt;  2) return false;\t\t\r\n\t\tif(Name.length() == 2) return true;\r\n\t\t\r\n\t\tString firstChars = Name.substring(0, 2);\r\n\t\tString lastChars  = Name.substring(Name.length()-2);\r\n\t\t\r\n\t\tif(firstChars == lastChars)\r\n\t\t  return true;\r\n\t\telse\r\n\t\t  return false;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>StringHelperTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n package com.mugil.junit.tutor;\r\n\r\nimport static org.junit.Assert.*;\r\nimport org.junit.Test;\r\n\r\npublic class StringHelperTest \r\n{\r\n  @Test\r\n  public void test() \r\n  {\r\n    StringHelper objStringHelper = new StringHelper();\r\n    assertTrue(objStringHelper.areFirstTwoandLastTwoStringsSame(&quot;AB&quot;));\r\n    assertFalse(objStringHelper.areFirstTwoandLastTwoStringsSame(&quot;ABAB&quot;));\r\n  }\r\n}\r\n<\/pre>\n<p>The above code checks whether First and Last two characters are the same and return true or false based on input to method areFirstTwoandLastTwoStringsSame.<\/p>\n<p><strong class=\"ctaHeader2\">@Before and @After Annotations<\/strong><br \/>\nThe before and after methods are used to run a case before and after the actual test started.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.junit.After;\r\nimport org.junit.Before;\r\nimport org.junit.Test;\r\n\r\npublic class StringHelperTest \r\n{\r\n\t\r\n  @Before\r\n  public void before()\r\n  {\t\r\n    System.out.println(&quot;Before Test&quot;);\r\n  }\r\n\t\r\n  @After\r\n  public void after()\r\n  {\t\r\n    System.out.println(&quot;After Test&quot;);\r\n  }\r\n\r\n  @Test\r\n  public void test1() \r\n  {\r\n    System.out.println(&quot;Test1&quot;);\r\n  }\r\n\t\r\n  @Test\r\n  public void test2() \r\n  {\r\n    System.out.println(&quot;Test2&quot;);\r\n  }\t\r\n}\r\n<\/pre>\n<p>The output of the above code would be<br \/>\n<strong>Output<\/strong><\/p>\n<pre>\r\nBefore Test\r\nTest1\r\nAfter Test\r\nBefore Test\r\nTest2\r\nAfter Test\r\n<\/pre>\n<p>You can also run a test before and after the class run as below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.junit.AfterClass;\r\nimport org.junit.BeforeClass;\r\n\r\n@BeforeClass\r\npublic static void beforeClass()\r\n{\t\r\n  System.out.println(&quot;Before Test&quot;);\r\n}\r\n\t\r\n@AfterClass\r\npublic static void afterClass()\r\n{\t\r\n  System.out.println(&quot;After Test&quot;);\r\n}\r\n\r\n@Test\r\npublic void test1() \r\n{\r\n  StringHelper objStringHelper = new StringHelper(); \r\n  System.out.println(&quot;During Test&quot;);\r\n}\r\n\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nBefore Test\r\nDuring Test\r\nAfter Test\r\n<\/pre>\n<p>BeforeClass and AfterClass is run only once when object for class is created.Note the static before beforeClass and afterClass methods.<\/p>\n<p>Typical Example for Using before and after methods where object for class created in before method and object assigned null in after method.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class StringHelperTest \r\n{\t\r\n  StringHelper objStringHelper1;\r\n\t\r\n  @Before\r\n  public void before()\r\n  {\t\r\n     objStringHelper1 = new StringHelper(); \r\n  }\r\n\t\r\n  @After\r\n  public void afterClass()\r\n  {\t\r\n    objStringHelper1 = null;\r\n  }\r\n\r\n  @Test\r\n  public void test1() \r\n  {   \r\n    System.out.println(&quot;Test Cases&quot;);\r\n  }\t\r\n}\r\n<\/pre>\n<p><strong class=\"ctaHeader2\">How to check expected array is returned in Output<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\nimport static org.junit.Assert.*;\r\nimport org.junit.Test;\r\n\r\npublic class StringHelperTest \r\n{\r\n  @Test\r\n  public void test1() \r\n  {\r\n    int&#x5B;] arrNums1 = {5,4,3,2,1};\r\n    Arrays.sort(arrNums1);\t\t\r\n    int&#x5B;] arrNums2 = {1,2,3,4,5};\r\n\t\t\r\n    assertArrayEquals(arrNums1, arrNums2);\r\n  }\r\n}\r\n<\/pre>\n<p>assertArrayEquals method comes from <strong>org.junit.Assert<\/strong> Package<\/p>\n<p>We are going to look for the expected exception is getting thrown or not<\/p>\n<p><strong class=\"ctaHeader2\">Testing for Exception<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Arrays;\r\nimport org.junit.Test;\r\n\r\npublic class StringHelperTest \r\n{\r\n  @Test(expected=NullPointerException.class)\r\n  public void test1() \r\n  {\r\n    int&#x5B;] arrNums1 = null;\r\n    Arrays.sort(arrNums1);\r\n  }\r\n}\r\n<\/pre>\n<p><strong class=\"ctaHeader2\">Testing for Performance<\/strong> &#8211; How Much time is taken by code block to execute the code.<\/p>\n<p>In the below code we are going to estimate actual time taken by the code block to execute<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n public class StringHelperTest \r\n {\r\n   @Test(timeout=100)\r\n   public void test1() \r\n   {\r\n     for (int i = 0; i &lt; 1000000; i++) \r\n     {\r\n\t    int&#x5B;] arrNumbs = {i-1, i, i+1};\r\n          Arrays.sort(arrNumbs);\r\n     }\r\n   }\r\n}\r\n<\/pre>\n<p><strong>timeout=100<\/strong> &#8211; is going to tell the code should complete the execution within 100 milliseconds.If it fails I should optimize the code by performance tweaks to boost codes performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The below examples are specific to Junit4 &lt;!&#8211; https:\/\/mvnrepository.com\/artifact\/junit\/junit &#8211;&gt; &lt;dependency&gt; &lt;groupId&gt;junit&lt;\/groupId&gt; &lt;artifactId&gt;junit&lt;\/artifactId&gt; &lt;version&gt;4.12&lt;\/version&gt; &lt;scope&gt;test&lt;\/scope&gt; &lt;\/dependency&gt; Basic Test case which Fails import static org.junit.Assert.*; import org.junit.Test; public class Junit1 { @Test public void basicTestFail() { fail(&quot;The Test Failed&quot;); } } There is No pass method in JUnit like fail method. As long as the test&hellip; <a href=\"https:\/\/codethataint.com\/blog\/junit-part1\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"class_list":["post-247","post","type-post","status-publish","format-standard","hentry","category-junit"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=247"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/247\/revisions"}],"predecessor-version":[{"id":4143,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/247\/revisions\/4143"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}