{"id":1782,"date":"2016-09-26T13:00:37","date_gmt":"2016-09-26T13:00:37","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1782"},"modified":"2016-09-26T17:32:53","modified_gmt":"2016-09-26T17:32:53","slug":"junit-tutorial-1","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/junit-tutorial-1\/","title":{"rendered":"Junit Tutorial 1"},"content":{"rendered":"<p><strong>Testing Output using assertEquals<\/strong><br \/>\n<strong>Scenario1.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Scenario\r\n{\t\r\n  public int Square(int no)\r\n  {\r\n    return no*no;\r\n  }\r\n}\r\n<\/pre>\n<p><strong>SquareTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SquareTest \r\n{\r\n  @Test\r\n  public void test() \r\n  {\r\n    int SquareNo = new Test1().Square(5);\t\t\r\n    assertEquals(25, SquareNo);\r\n  }\r\n}\r\n<\/pre>\n<p><strong>Scenario2.java(word Counter)<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Scenario2 \r\n{\t\r\n public int repeatedWords(String pWord)\r\n {\r\n   int count = 0;\r\n\t\r\n   for (int j = 0; j &lt; pWord.length(); j++) \r\n   {\r\n     if(pWord.charAt(j) == 'a')\r\n     {\r\n       count++;\t\r\n     }\r\n   }\t\r\n    return count;\r\n }\r\n}\r\n<\/pre>\n<p><strong>RepeatTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class RepeatTest {\r\n   @Test\r\n   public void test() {\r\n     Scenario2 objScenario2 = new Scenario2();\t\t\r\n     assertEquals(objScenario2.repeatedWords(&quot;alphabet&quot;), 2);\t\t\r\n   }\r\n}\r\n<\/pre>\n<p>Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @SuiteClasses annotations are used to run the suite tests.<\/p>\n<p><strong>AllTests.java (TestSuite)<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@RunWith(Suite.class)\r\n@SuiteClasses({ RepeatTest.class, SquareTest.class })\r\npublic class AllTests {\r\n\r\n}\r\n<\/pre>\n<p>Now Lets take a Real Life Scenario of Bank Account<br \/>\n<strong>Account.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Account \r\n{\r\n\tpublic int Balance;\r\n\t\r\n\tpublic Account()\r\n\t{\r\n\t\tBalance = 0;\r\n\t}\r\n\t\r\n\tpublic int getAccBalance()\r\n\t{\r\n\t\treturn Balance;\r\n\t}\r\n\t\r\n\tpublic void getCash(int pCash)\r\n\t{\r\n\t\tBalance = Balance - pCash;\r\n\t}\r\n\t\r\n\tpublic void putCash(int pCash)\r\n\t{\r\n\t\tBalance = Balance + pCash;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>BankTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class BankTest \r\n{\r\n\tAccount objAcc = new Account();\r\n\t\r\n\t@Test\r\n\tpublic void checkAccBalanceTest()\r\n\t{\r\n\t\tassertEquals(objAcc.getAccBalance(), 0);\r\n\t}\r\n\t\r\n\t@Test\r\n\tpublic void checkBalAfterDepositTest()\r\n\t{\r\n\t\tobjAcc.putCash(50);\r\n\t\tassertEquals(objAcc.getAccBalance(), 50);\r\n\t}\r\n\t\r\n\t@Test\r\n\tpublic void checkBalAfterWithdrawTest()\r\n\t{\r\n\t\tobjAcc.getCash(30);\t\t\r\n\t\tassertEquals(objAcc.getAccBalance(), 20);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Points to Note<\/strong><\/p>\n<ol>\n<li>The methods in BankTest ends with Test Suffix.This ensures the test cases are executed in order.<\/li>\n<li>The Account.java and BankTest.java are two different projects.In BankTest project the other project is added in <em>Java Build Path<\/em><\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/09\/Junit1.png\" alt=\"\" height=\"597\" width=\"308\"\/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Testing Output using assertEquals Scenario1.java public class Scenario { public int Square(int no) { return no*no; } } SquareTest.java public class SquareTest { @Test public void test() { int SquareNo = new Test1().Square(5); assertEquals(25, SquareNo); } } Scenario2.java(word Counter) public class Scenario2 { public int repeatedWords(String pWord) { int count = 0; for (int j&hellip; <a href=\"https:\/\/codethataint.com\/blog\/junit-tutorial-1\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[206],"tags":[],"class_list":["post-1782","post","type-post","status-publish","format-standard","hentry","category-basics"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1782","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=1782"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1782\/revisions"}],"predecessor-version":[{"id":1788,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1782\/revisions\/1788"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}