{"id":1293,"date":"2015-06-23T11:47:09","date_gmt":"2015-06-23T15:47:09","guid":{"rendered":"http:\/\/springframework.guru\/?p=1293"},"modified":"2019-06-24T21:14:30","modified_gmt":"2019-06-25T01:14:30","slug":"unit-testing-junit-part-1","status":"publish","type":"post","link":"https:\/\/springframework.guru\/unit-testing-junit-part-1\/","title":{"rendered":"Unit Testing with JUnit &#8211; Part 1"},"content":{"rendered":"<p>Unit testing is the first level of <a title=\"Testing Software\" href=\"http:\/\/springframework.guru\/testing-software\/\" target=\"_blank\" rel=\"noopener noreferrer\">testing software<\/a> where you write test code that executes a specific functionality in the code to be tested. In most cases, you as a programmer are responsible to deliver unit tested code. The objective is to check if the unit of the software, for example a public method of a class under test, behaves as expected and\/or returns the expected data. Unit tests are <em>not<\/em> done on the production system but as isolated units. If the unit under test have external dependencies, such as an external data source or a Web service, the dependencies are replaced with a test implementation or a mock object created using a test framework. Unit testing is not the only type and it alone cannot handle all testing aspects. Other types of testing, such as integration and functional testing have their own roles in testing software.<\/p>\n<p>In this series of posts we will focus on unit testing with JUnit &#8211; one of the most popular framework to test Java code. In this post, we will start by creating and executing a basic unit test, and then in further posts move to specific unit testing aspects.<\/p>\n<p>The core JUnit framework comes in a single JAR file, which you can download, point the classpath to it, and then create and run tests. But in this post, we will learn how to perform unit testing in the real programmer\u2019s way. We will start with Maven, and then move to IntelliJ.<\/p>\n<h2>Unit Testing with Maven<\/h2>\n<p>You&#8217;ve likely heard Maven being referred as a build tool. But, in addition to its capability to build deployable artifacts from source code, Maven provides a number of features for managing the software development life cycle. Unit testing is one such feature, which is incorporated as a test phase in the <a title=\"Maven Build Lifecycle\" href=\"https:\/\/maven.apache.org\/guides\/introduction\/introduction-to-the-lifecycle.html\" target=\"_blank\" rel=\"noopener noreferrer\">Maven build lifecycle<\/a>.<\/p>\n<p>Without going into Maven in depth, let\u2019s start our first JUnit test with Maven.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li><a title=\"Download and Install Maven\" href=\"https:\/\/maven.apache.org\/download.cgi\" target=\"_blank\" rel=\"noopener noreferrer\">Download and install Maven<\/a>\u00a0if you haven&#8217;t done yet.<\/li>\n<li>Open up a command prompt (Windows) or a terminal (*uix or Mac), browse to a working directory to setup the project, and execute the following command.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">mvn archetype:generate -DgroupId=guru.springframework.unittest.quickstart -DartifactId=unittest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false<\/pre>\n<p>The preceding <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">archetype:generate<\/code> command uses the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">maven-archetype-quickstart<\/code> template to create a basic Maven project containing a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">pom.xml<\/code> file, a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">App.java<\/code> class, and a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">AppTest.java<\/code> test class in the following directory structure.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">\u2514\u2500\u2500\u2500pom.xml      \r\n\u2514\u2500\u2500\u2500src\r\n    \u251c\u2500\u2500\u2500main\r\n    \u2502   \u2514\u2500\u2500\u2500java\r\n    \u2502       \u2514\u2500\u2500\u2500guru\r\n    \u2502           \u2514\u2500\u2500\u2500springframework\r\n    \u2502               \u2514\u2500\u2500\u2500unittest\r\n    \u2502                   \u2514\u2500\u2500\u2500quickstart\r\n    \u2502                           App.java\r\n    \u2502                           \r\n    \u2514\u2500\u2500\u2500test\r\n        \u2514\u2500\u2500\u2500java\r\n            \u2514\u2500\u2500\u2500guru\r\n                \u2514\u2500\u2500\u2500springframework\r\n                    \u2514\u2500\u2500\u2500unittest\r\n                        \u2514\u2500\u2500\u2500quickstart\r\n                                AppTest.java\r\n<\/pre>\n<p>In the directory structure above, the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">pom.xml<\/code> file, also known as the Maven configuration file is the heart of a Maven project. It is where you define your project configurations &#8211; specifically the dependencies of your project. For example, as our project depends on JUnit, we need to declare it as a dependency in the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">pom.xml<\/code> file. Although a JUnit dependency will already be present by default, we will update it to point to the latest JUnit version. This is how our final <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">pom.xml<\/code> file will look like.<\/p>\n<h4>pom.xml<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">  4.0.0\r\n  guru.springframework.unittest.quickstart\r\n  unittest\r\n  jar\r\n  1.0-SNAPSHOT\r\n  unittest\r\n  http:\/\/maven.apache.org\r\n  \r\n    \r\n     junit\r\n     junit\r\n     4.12\r\n     test\r\n    \r\n      \r\n          org.hamcrest\r\n          hamcrest-library\r\n          1.3\r\n          test\r\n      \r\n      \r\n          org.mockito\r\n          mockito-all\r\n          1.9.5\r\n      \r\n  \r\n\r\n<\/pre>\n<p>Now that we have set up a basic Java class, a test class, and the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">pom.xml<\/code> configuration, we can run a unit test.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"3\">\n<li>Execute the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">mvn test<\/code> command from the working directory.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>This command will run the default <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">AppTest<\/code> class that Maven generated for us with the following output.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">-------------------------------------------------------\r\n T E S T S\r\n-------------------------------------------------------\r\nRunning guru.springframework.unittest.quickstart.AppTest\r\nTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.043 sec\r\n\r\nResults :\r\n\r\nTests run: 1, Failures: 0, Errors: 0, Skipped: 0\r\n\r\n[INFO] ------------------------------------------------------------------------\r\n[INFO] BUILD SUCCESS\r\n[INFO] ------------------------------------------------------------------------\r\n[INFO] Total time: 8.691 s\r\n[INFO] Finished at: 2015-06-18T20:16:34+05:30\r\n[INFO] Final Memory: 14M\/136M\r\n[INFO] ------------------------------------------------------------------------ \r\n<\/pre>\n<p>We have executed a JUnit test using Maven. This test passed, but hardly provides any value yet. We will next move to using the IntelliJ IDE to write and execute a more comprehensive test.<\/p>\n<h2>Unit Testing in IntelliJ<\/h2>\n<p>Using IntelliJ, you can easily create, run, and debug unit tests. Among several other unit testing frameworks, IntelliJ provides built-in support for JUnit. In IntelliJ, you can create a JUnit test class with a click and navigate quickly between test classes and their corresponding target classes to debug test errors. A very useful unit testing feature in IntelliJ is code coverage. With this feature, you can view the exact percentage of methods and even lines of code covered by unit tests in your project.<\/p>\n<p>Let\u2019s import our existing Maven project to IntelliJ and do some unit testing.<\/p>\n<h3>Import Maven Project to IntelliJ<\/h3>\n<p>If you don&#8217;t have IntelliJ installed, download and install the free Community Edition or the 30-day trial of Ultimate Edition from the <a title=\"Download IntelliJ\" href=\"https:\/\/www.jetbrains.com\/idea\/download\/\" target=\"_blank\" rel=\"noopener noreferrer\">official website<\/a>. Once you are done, perform the following steps:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Open IntelliJ.<\/li>\n<li>On the <strong>Welcome to IntelliJ IDEA<\/strong> window, click <strong>Import Project<\/strong>.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img01-lg-500X347.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1299 aligncenter\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img01-lg-500X347.png\" alt=\"Click Import project\" width=\"676\" height=\"470\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img01-lg-500X347.png 676w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img01-lg-500X347-300x209.png 300w\" sizes=\"(max-width: 676px) 100vw, 676px\" \/><\/a><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"3\">\n<li>In the <strong>Select File or Directory to Import<\/strong> dialog box, browse to the working directory of the Maven project and select the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">pom.xml<\/code> file.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img02-lg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1301\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img02-lg.png\" alt=\"Select pom.xml\" width=\"440\" height=\"578\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img02-lg.png 440w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img02-lg-228x300.png 228w\" sizes=\"(max-width: 440px) 100vw, 440px\" \/><\/a><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"4\">\n<li>Click the <strong>OK <\/strong>button.<\/li>\n<li>In the <strong>Import Project from Maven<\/strong> dialog box that appears, select the <strong>Import Maven projects automatically<\/strong> checkbox to synchronize changes between the Maven and InteliiJ projects each time the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">pom.xml<\/code> file changes.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img03-lg-500X487.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1314\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img03-lg-500X487.png\" alt=\"Select the Import Maven projects automatically checkbox \" width=\"715\" height=\"697\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img03-lg-500X487.png 715w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img03-lg-500X487-300x292.png 300w\" sizes=\"(max-width: 715px) 100vw, 715px\" \/><\/a><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"6\">\n<li>Click the <strong>Next <\/strong>button through a few more dialog boxes, accepting the default settings, and finally click <strong>Finish<\/strong>. The <strong>Project <\/strong>window of IntelliJ displays the project structure.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img04-lg500-247.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1302\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img04-lg500-247.png\" alt=\"Thct Structure in the IntelliJ Project Windowe Proje\" width=\"1133\" height=\"560\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img04-lg500-247.png 1133w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img04-lg500-247-300x148.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img04-lg500-247-1024x506.png 1024w\" sizes=\"(max-width: 1133px) 100vw, 1133px\" \/><\/a><\/p>\n<ol start=\"7\">\n<li>Double click <strong>App<\/strong> in the <strong>Project<\/strong> window to open it in the code editor.<\/li>\n<li>Replace the default code of the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">App<\/code> class with this code.<\/li>\n<\/ol>\n<h4>App.java<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">package guru.springframework.unittest.quickstart;\r\n\r\n\r\npublic class App\r\n{\r\n    public String concatAndConvertString(String str1, String str2){\r\n        String concatedString=str1.concat(str2);\r\n        return concatedString.toUpperCase();\r\n    }\r\n    }\r\n<\/pre>\n<p>In the code above we wrote a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">concatAndConvertString()<\/code> method in the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">App<\/code> class that accepts two <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">String<\/code> parameters. The method first concatenates the strings and converts the result to uppercase before returning it.<\/p>\n<p>We will next add a test class to test the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">concatAndConvertString()<\/code> method.<\/p>\n<h3>Add a Test Class<\/h3>\n<p>Let\u2019s go through the steps to add a test class in IntelliJ from scratch.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Delete the default <strong>AppTest<\/strong> class from the\u00a0<b>Project<\/b>\u00a0window.<\/li>\n<li>In the <strong>Project<\/strong> window create a directory with the name <strong>test<\/strong> under <strong>main<\/strong>, We will use the <strong>test<\/strong> directory to keep the test code separated from the application code.<\/li>\n<li>Right-click <strong>test<\/strong> and select <strong>Mark Directory As\u2192Test Sources Root<\/strong>.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img05-lg-800X444.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1310\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img05-lg-800X444-1024x568.png\" alt=\"select Mark Directory As, and then Test Sources Root.\" width=\"863\" height=\"479\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img05-lg-800X444-1024x568.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img05-lg-800X444-300x167.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img05-lg-800X444.png 1243w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"4\">\n<li>In the code editor where the <strong>App<\/strong> class is open, press <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">Shift+F10<\/code> and select <strong>Create New Test<\/strong>.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img06CreateNewTest.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1305\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img06CreateNewTest.png\" alt=\"Select Create New Test\" width=\"594\" height=\"223\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img06CreateNewTest.png 594w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img06CreateNewTest-300x113.png 300w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/a><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"5\">\n<li>In the<strong> Create Test<\/strong> dialog box that appears, select the <strong>jUnit4<\/strong> radio button and the check box corresponding to the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">concatAndConvertString()<\/code> method that we will test.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img06.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1306\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img06.png\" alt=\"Select jUnit4 radio button and the method that we will test\" width=\"574\" height=\"527\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img06.png 574w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img06-300x275.png 300w\" sizes=\"(max-width: 574px) 100vw, 574px\" \/><\/a><\/p>\n<ol start=\"6\">\n<li>Click the <strong>OK<\/strong> button. JUnit creates the <strong>AppTest<\/strong> class with a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">testConcatAndConvertString()<\/code> method decorated with the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@Test<\/code> annotation. This annotation tells JUnit to run the method as a test case. In the test method, we will write the code to test the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">concatAndConvertString()<\/code> method of <strong>App<\/strong>.<\/li>\n<\/ol>\n<h4>AppTest.java<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">package guru.springframework.unittest.quickstart;\r\nimport org.junit.Test;\r\nimport static org.junit.Assert.*;\r\n\r\npublic class AppTest {\r\n\r\n   @Test\r\n    public void testConcatAndConvertString() throws Exception {\r\n       String expectedValue=\"HELLOWORLD\";\r\n       App app=new App();\r\n       String actualValue=app.concatAndConvertString(\"Hello\", \"World\");\r\n       assertEquals(expectedValue, actualValue);\r\n    }\r\n}\r\n\r\n\r\n\r\n<\/pre>\n<p>In Line 12 of the example above, we called the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">assertEquals()<\/code> method, which is one of the several JUnit assertion methods. This overloaded method checks whether two <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">String<\/code> objects are equal. If they are not, the method throws an <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">AssertionError<\/code>. In our example, we called the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">assertEquals()<\/code> method by passing the expected string value (<code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">HELLOWORLD<\/code>) as the first parameter and the actual value that the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">concatAndConvertString()<\/code> method returns as the second parameter.<\/p>\n<h3>Run the Unit Test<\/h3>\n<p>To run the test, select <strong>Run AppTest<\/strong> from the Run menu of IntelliJ or press <strong>Shift+F10<\/strong>. The <strong>Run<\/strong> window displays the test result. A green highlight box indicates that the test completed without any failure.<\/p>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img07-800X461.png\"><img loading=\"lazy\" decoding=\"async\" class=\"ligncenter size-large wp-image-1308 aligncenter\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img07-800X461.png\" alt=\"Green highlight box indicating a passed test\" width=\"800\" height=\"461\" \/><\/a><\/p>\n<p>To know how test failures are reported, change the value of the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">expectedValue <\/code>variable to <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">HelloWorld<\/code> and press <strong>Shift+F10<\/strong>. The <strong>Run<\/strong> dialog box displays a red progress bar to indicate the test failure along with a comparison failure message.<\/p>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img08-800X468.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1308\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img08-800X468-1024x599.png\" alt=\"Red Progress Bar Indicating a Failed Test\" width=\"800\" height=\"468\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img08-800X468-1024x599.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img08-800X468-300x176.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/06\/Img08-800X468.png 1133w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n<p>Revert the the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">expectedValue<\/code> variable to its original value before you close IntelliJ.<\/p>\n<h2>Summary<\/h2>\n<p>At this point, if you are thinking <em>Why not just use System.out.println() for unit testing?<\/em>\u00a0Then you are thinking wrong. Inserting <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">System.out.println()<\/code> for debugging into code is undesirable because it requires manually scanning the output, every time the program is run, to ensure that the code is doing what&#8217;s expected. Imagine doing this in an enterprise application having hundreds and thousands of lines of code. On the other hand, unit tests examine the code&#8217;s behavior on runtime from the client&#8217;s point of view. This provides better insight on what might happen when the software is released.<\/p>\n<p>Whenever you are writing code, you should be writing unit tests too. Writing unit tests will catch programming errors and improve the quality of your code. Many professional developers advocate doing Test Driven Development (TDD), where you write your unit tests before you write the application code.<\/p>\n<p>Either way if you write your unit tests before or after you write the application code, the unit tests become a valuable asset for instrumenting your code. As the code base grows, you can refactor things as needed, and have more confidence that your changes will not have unintended consequences (ie where you change one thing, and accidently break something else).<\/p>\n<p>In <a href=\"http:\/\/springframework.guru\/unit-testing-junit-part-2\/\">part 2 of my tutorial series on unit testing with JUnit<\/a> I&#8217;ll take a deeper look at JUnit Assertions, JUnit Annotations, and JUnit Test Suites.<\/p>\n<h3>Unit Testing with\u00a0the Spring Framework<\/h3>\n<p>Testing is an integral part of <a title=\"Enterprise Application Development\" href=\"http:\/\/springframework.guru\/using-the-spring-framework-for-enterprise-application-development\/\" target=\"_blank\" rel=\"noopener noreferrer\">Enterprise Application Development<\/a>\u00a0process with the Spring Framework. The architecture of the Spring Framework lends itself to modular code and easier unit testing. \u00a0Spring provides testing support through the TestContext Framework that abstracts the underlying testing framework, such as JUnit and TestNG. You can use it by setting <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">SpringJUnit4ClassRunner.class<\/code> as the value for the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@RunWith<\/code> annotation. This tells Spring to use the test runner of TestContext instead of JUnit\u2019s built in test runner.\u00a0I&#8217;ve written a more in depth post about testing Spring applications with JUnit <a href=\"http:\/\/springframework.guru\/integration-testing-with-spring-and-junit\/\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unit testing is the first level of testing software where you write test code that executes a specific functionality in the code to be tested. In most cases, you as a programmer are responsible to deliver unit tested code. The objective is to check if the unit of the software, for example a public method [&hellip;]<a href=\"https:\/\/springframework.guru\/unit-testing-junit-part-1\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":4591,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Unit Testing with JUnit - Part 1 https:\/\/wp.me\/p5BZrZ-kR #junit #java","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[20,33,85],"tags":[43,27,60,28,113],"class_list":["post-1293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-junit","category-testing","tag-intellij","tag-java","tag-junit","tag-maven","tag-unit-test"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"Simanta","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/03\/Banner560x292_06web.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-kR","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/1293"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=1293"}],"version-history":[{"count":10,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/1293\/revisions"}],"predecessor-version":[{"id":5727,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/1293\/revisions\/5727"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4591"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=1293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=1293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=1293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}