{"id":1075,"date":"2014-09-04T06:23:22","date_gmt":"2014-09-04T10:23:22","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=1075"},"modified":"2015-04-12T08:41:17","modified_gmt":"2015-04-12T12:41:17","slug":"junit-tutorial-beginners","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/junit-tutorial-beginners\/","title":{"rendered":"JUnit: For beginners by a beginner"},"content":{"rendered":"<p><strong>Problem:<\/strong> JUnit tutorials are often written by testers with a reasonable amount of unit checking expertise<\/p>\n<h3>Why this post?<\/h3>\n<p>There are plenty of JUnit tutorials available online. They are often written by testers with a reasonable amount of unit checking experience. The problem is that experts and beginners see things differently. I began noticing that beginners often read these JUnit tutorials written by experts and come away feeling like there is a unsurmountable mountain of knowledge they need to learn before applying the concepts. So <a href=\"http:\/\/www.qxf2.com\">we<\/a> decided to try something different &#8211; writing tutorials for beginners by beginners. Rupesh Mishra, a Qxf2 Services employee, volunteered to write this post. The rest of the post is written by Rupesh.<\/p>\n<h3>Getting started with JUnit<\/h3>\n<p>Using JUnit you can write unit test cases for Java programs, run the test cases and assert the test output with expected result. JUnit makes it easy and fast for me to write unit tests. This post will explain how to create and\u00a0execute\u00a0JUnit test cases in Eclipse. At a high level, these are the steps we cover:<br \/>\n1. Create a project in Eclipse<br \/>\n2. Write a Java program to use as the program under test<br \/>\n3. Create test file<br \/>\n4. Write the JUnit test<br \/>\n5. Execute the test and view results<\/p>\n<p><strong>1. Create a project in Eclipse<\/strong><br \/>\nTo write your first Java program using Eclipse your system should have an updated version of Eclipse. You can download and install Eclipse from <a href=\"https:\/\/www.eclipse.org\/downloads\/\">here<\/a>.<br \/>\nNow in Eclipse IDE, to create your first Java project click on File -&gt; New -&gt; Java Project.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/CreateJavaProject.jpg\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1316\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/CreateJavaProject-223x300.jpg\" alt=\"CreateJavaProject\" width=\"223\" height=\"300\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/CreateJavaProject-223x300.jpg 223w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/CreateJavaProject.jpg 533w\" sizes=\"auto, (max-width: 223px) 100vw, 223px\" \/><\/a><\/p>\n<p>Now I got <span style=\"text-decoration: underline;\">New Java Project<\/span> window to create a project.<\/p>\n<ul>\n<li>Give a name of your project. Here I am giving \u00a0<span style=\"text-decoration: underline;\">javatesting<\/span><\/li>\n<li>Tick mark the check-box for &#8220;Use default location&#8221;.<\/li>\n<li>Under JRE section select radio button for &#8220;Use an execution environment JRE&#8221;.<\/li>\n<li>Under Project layout section select radio button for &#8220;Create separate folders for sources and class files&#8221;.<\/li>\n<li>\u00a0Keep remaining as it is. Click Finish.<\/li>\n<\/ul>\n<p>To create a new Class right click the Project. For me project name is <span style=\"text-decoration: underline;\">javatesting<\/span>.<br \/>\nChoose New -&gt; Class,<br \/>\nIn the <span style=\"text-decoration: underline;\">New Java Class<\/span> window<\/p>\n<ul>\n<li>Source folder should be &#8220;Project name \/ src&#8221; like-javatesting\/src.<\/li>\n<li>Normally package name will be same as project name.<\/li>\n<li>Put the name of class,I put <span style=\"text-decoration: underline;\">practice_code.java<\/span><\/li>\n<li>Keep remaining as it is. Click Finish.<\/li>\n<\/ul>\n<p style=\"text-align: center;\"><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/NewJavaClass.jpg\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1325\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/NewJavaClass-254x300.jpg\" alt=\"NewJavaClass\" width=\"254\" height=\"300\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/NewJavaClass-254x300.jpg 254w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/NewJavaClass.jpg 542w\" sizes=\"auto, (max-width: 254px) 100vw, 254px\" \/><\/a><\/p>\n<p><strong>2. Write a Java program to use as the program under test<\/strong><br \/>\nHere I created a very simple Java program in <span style=\"text-decoration: underline;\">practice_code.java<\/span> under project name <span style=\"text-decoration: underline;\">javatesting<\/span> to test with JUnit.<\/p>\n<pre lang=\"java\">package javatesting;\r\n\r\npublic class Practice_code {\r\n\t\/*\r\n\t * Remove\u00a0character 'A' if it comes in the first two places of the string\r\n         * Examples:\r\n\t * \"ABCD\"=&gt;\"BCD\"\r\n\t * \"AACD\"=&gt;\"CD\"\r\n\t * \"BACD\"=&gt;\"BCD\"\r\n\t * \"AAAA\"=&gt;\"AA\"\r\n\t *\/\r\n\tpublic String truncateAinFirst2pos(String str){\r\n\t\tif (str.length()&lt;=2)\r\n\t\t\treturn str.replace(\"A\", \"\");\r\n\t\tString first2Chars = str.substring(0,2);\r\n\t\tString stringMinusFirst2Chars = str.substring(2);\r\n\t\treturn first2Chars.replaceAll(\"A\", \"\") + stringMinusFirst2Chars;\r\n\t}\r\n}\r\n<\/pre>\n<p>In this program I am truncating\u00a0character &#8216;A&#8217; if it comes to the first two places of the string.<\/p>\n<p><strong>3. Create test file<\/strong><br \/>\nIn order to test this method using JUnit,\u00a0I must add JUnit to the path, create a test class and implement a test. Eclipse has JUnit support and we can use it. But before creating a test class its good practice to create a new source folder dedicated just to test. Right click to project name <span style=\"text-decoration: underline;\">javatesting<\/span>, choose New-&gt;source folder, name it <span style=\"text-decoration: underline;\">test<\/span> and click to finish.<br \/>\nTo create test class right click to\u00a0<span style=\"text-decoration: underline;\">practice_code.java<\/span>,choose New-&gt; JUnit Test Case. In the <span style=\"text-decoration: underline;\">New JUnit Test Case<\/span> window<\/p>\n<ul>\n<li>Select radio button for &#8216;New JUnit 4 test&#8217;<\/li>\n<li>Change the Source folder location from javatesting\/src to javatesting\/test<\/li>\n<li>Name of the this test class will automatically be your Java class name + Test. You can change it if you have to create multiple test classes for the same Java class.<\/li>\n<\/ul>\n<p><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/testclass.jpg\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1185 aligncenter\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/testclass-300x288.jpg\" alt=\"Junit_testclass\" width=\"300\" height=\"288\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/testclass-300x288.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/testclass.jpg 625w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Keep remaining options as it is and click <span style=\"text-decoration: underline;\">Next<\/span>.<br \/>\nNow you can select methods for which you want to create your tests. I am selecting my only method &#8220;truncateAinFirst2pos(String )&#8221; under class <span style=\"text-decoration: underline;\">Practice_code<\/span>. Click to finish.<\/p>\n<div style=\"text-align: center;\"><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/selectMethod.jpg\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1193\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/selectMethod-260x300.jpg\" alt=\"selectMethod\" width=\"260\" height=\"300\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/selectMethod-260x300.jpg 260w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/selectMethod.jpg 525w\" sizes=\"auto, (max-width: 260px) 100vw, 260px\" \/><\/a><\/div>\n<p>If this is the first time you are doing this operation, Eclipse will ask you to add JUnit library to your class path. Click OK.<\/p>\n<p><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/junitask.jpg\" data-rel=\"lightbox-image-4\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1194 aligncenter\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/junitask-300x157.jpg\" alt=\"junitask\" width=\"300\" height=\"157\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/junitask-300x157.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/junitask.jpg 529w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now I got my test class file <span style=\"text-decoration: underline;\">Practice_codeTest.java<\/span> into the test folder.<\/p>\n<p><strong>4. Write the JUnit test <\/strong><br \/>\nEclipse already created a class named <span style=\"text-decoration: underline;\">Practice_codeTest<\/span> with method testTruncateAinFirst2pos() in our test file\u00a0<span style=\"text-decoration: underline;\">Practice_codeTest.java<\/span>.<\/p>\n<pre lang=\"java\">package javatesting;\r\nimport static org.junit.Assert.*;\r\nimport org.junit.Test;\r\n\r\npublic class Practice_codeTest {\r\n\t@Test\r\n\tpublic final void testTruncateAinFirst2pos() {\r\n\t\tfail(\"Not yet implemented\"); \/\/ TODO\r\n\t}\r\n}\r\n<\/pre>\n<p>Here Eclipse has already imported &#8216;junit.Test&#8217; to test the method and &#8216;junit.Assert&#8217; to compare the test output with expected result.<\/p>\n<p>Lets put some condition in our method to test our class. Create a instance &#8216;InputString&#8217; for the class <span style=\"text-decoration: underline;\">Practice_code<\/span>.<\/p>\n<pre lang=\"java\">Practice_code InputString = new Practice_code();\r\n<\/pre>\n<p>Now I am going to use one of the JUnit methods named <strong>assertEquals<\/strong> which allows me to compare \u00a0expected output with actual output.<\/p>\n<pre lang=\"java\">\/\/assertEquals(\"expected output\",\"actual output\" );\r\nassertEquals(\"BCD\", InputString.truncateAinFirst2pos(\"ABCD\"));\r\n<\/pre>\n<p>Here is my complete\u00a0<span style=\"text-decoration: underline;\">Practice_codeTest.java<\/span> file to test the method truncateAinFirst2pos() in <span style=\"text-decoration: underline;\">Practice_code.java<\/span> file.<\/p>\n<pre lang=\"java\">package javatesting;\r\nimport static org.junit.Assert.*;\r\nimport org.junit.Test;\r\n\r\npublic class Practice_codeTest {\r\n\t@Test\r\n\tpublic final void testTruncateAinFirst2pos() {\r\n\t\tPractice_code InputString = new Practice_code();\r\n\t\tassertEquals(\"BCD\", InputString.truncateAinFirst2pos(\"ABCD\"));\r\n\t}\r\n}\r\n<\/pre>\n<p>My project files structure is looking like this.<\/p>\n<p><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/Capture123.jpg\" data-rel=\"lightbox-image-5\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1228 aligncenter\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/Capture123.jpg\" alt=\"ProjectStructure\" width=\"234\" height=\"168\" \/><\/a><\/p>\n<p><strong>5. Execute the test and view results<\/strong><br \/>\nFor execution right click on your JUnit class file, in my case <span style=\"text-decoration: underline;\">Practice_codeTest.java<\/span> and choose Run As-&gt;JUnit Test.<\/p>\n<p><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/result.jpg\" data-rel=\"lightbox-image-6\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1238 aligncenter\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/result-253x300.jpg\" alt=\"result\" width=\"253\" height=\"300\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/result-253x300.jpg 253w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/result.jpg 343w\" sizes=\"auto, (max-width: 253px) 100vw, 253px\" \/><\/a><\/p>\n<p>Here is Junit runner window, which shows <strong>Errors 0 and Failures 0<\/strong> that means all pass.<\/p>\n<p>To show failure condition I am making some changes in my expected output.<\/p>\n<pre lang=\"java\">\/\/assertEquals(\"expected output\",\"actual output\" );\r\nassertEquals(\"ABCD\", InputString.truncateAinFirst2pos(\"ABCD\"));\r\n<\/pre>\n<p>Now, as expected, in JUnit runner window I got <strong>Failures 1<\/strong>.JUnit clearly points out whats wrong in your code under Failure Trace as its showing here &#8220;expected:&lt;[A]BCD&gt; but was:&lt;[]BCD&gt;&#8221;.<\/p>\n<p><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/FailuresResult.jpg\" data-rel=\"lightbox-image-7\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1243 aligncenter\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/FailuresResult-300x287.jpg\" alt=\"FailuresResult\" width=\"300\" height=\"287\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/FailuresResult-300x287.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/08\/FailuresResult.jpg 510w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>If I double click the Failure Trace line it will show the Result Comparison window.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/ResultComparison.jpg\" data-rel=\"lightbox-image-8\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1341\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/ResultComparison-300x111.jpg\" alt=\"ResultComparison\" width=\"300\" height=\"111\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/ResultComparison-300x111.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/ResultComparison.jpg 739w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<hr \/>\n<p>And that brings us to the end of the tutorial. Let us know if it helped having a beginner write for beginners. Stay tuned for an upcoming post on <strong><a href=\"http:\/\/qxf2.com\/blog\/juint-asserts-and-matchers\/\">Junit asserts and matchers<\/a><\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: JUnit tutorials are often written by testers with a reasonable amount of unit checking expertise Why this post? There are plenty of JUnit tutorials available online. They are often written by testers with a reasonable amount of unit checking experience. The problem is that experts and beginners see things differently. I began noticing that beginners often read these JUnit [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,54,61,66],"tags":[],"class_list":["post-1075","post","type-post","status-publish","format-standard","hentry","category-java","category-junit","category-testing","category-unit-testing"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=1075"}],"version-history":[{"count":105,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1075\/revisions"}],"predecessor-version":[{"id":1544,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1075\/revisions\/1544"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=1075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=1075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=1075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}