{"id":87265,"date":"2020-04-03T11:00:00","date_gmt":"2020-04-03T08:00:00","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=87265"},"modified":"2022-07-07T15:05:27","modified_gmt":"2022-07-07T12:05:27","slug":"java-unit-testing-with-junit-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/","title":{"rendered":"Java Unit Testing with JUnit Example"},"content":{"rendered":"<p>This is an article for <a href=\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-tutorial\/\">Java Unit Testing<\/a> with JUnit Example.<\/p>\n<p>You can also check this tutorial in the following video:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/www.youtube.com\/watch?v=__oDoJ94gmc&amp;feature=youtu.be\"><img decoding=\"async\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/JUnit-Testing-in-Java-1024x576.jpg\" alt=\"\" class=\"wp-image-113852\" width=\"512\" height=\"288\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/JUnit-Testing-in-Java-1024x576.jpg 1024w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/JUnit-Testing-in-Java-300x169.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/JUnit-Testing-in-Java-768x432.jpg 768w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/JUnit-Testing-in-Java.jpg 1280w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><figcaption>JUnit Testing in Java &#8211; video<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-introduction\">1. Introduction<\/h2>\n<p>Java unit testing is a software testing where methods and classes are tested.  <a rel=\"noreferrer noopener\" href=\"https:\/\/junit.org\/\" target=\"_blank\">JUnit<\/a> is a unit testing framework for the Java programming language which provides a way to test the application as many as you want. Unit testing usually includes the following steps:<\/p>\n<ol class=\"wp-block-list\">\n<li>define a test<\/li>\n<li>create an instance of the testing class<\/li>\n<li>prepare the test data<\/li>\n<li>execute a test<\/li>\n<li>verify the testing results<\/li>\n<li>report the testing results<\/li>\n<\/ol>\n<p>JUnit supports step 1 via <code>@Test<\/code> annotation, step 4 via <code>@RunWith<\/code> annotation, and step 5 via assertion API. In this example, I will create a multi-module maven project to demonstrate how to utilize the JUnit framework to create a test class.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-2-technologies-used\">2. Technologies Used<\/h2>\n<p>The example code in this article was built and run using:<\/p>\n<ul class=\"wp-block-list\">\n<li>Java 11<\/li>\n<li>Maven 3.3.9<\/li>\n<li>Eclipse Oxygen<\/li>\n<li>JUnit (4 and 5)<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"h-3-maven-multi-modules-project\">3. Maven Multi-Modules Project<\/h2>\n<p><a rel=\"noreferrer noopener\" aria-label=\"JUnit 5 (opens in a new tab)\" href=\"https:\/\/junit.org\/junit5\/\" target=\"_blank\">JUnit 5<\/a> was released in 2017. It is not backwards compatible with <a rel=\"noreferrer noopener\" aria-label=\"JUnit 4 (opens in a new tab)\" href=\"https:\/\/junit.org\/junit4\/\" target=\"_blank\">JUnit 4<\/a> which released in 2006. In this step, I will demonstrate both JUnit 4 and JUnit 5 in a three-module Maven project:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>common<\/code> &#8211; includes a main class &#8211; <code>SomeClass<\/code>.<\/li>\n<li><code>JUnit4-demo<\/code> &#8211; tests <code>SomeClass<\/code> with JUnit 4.<\/li>\n<li><code>JUnit5-demo<\/code> &#8211; tests <code>SomeClass<\/code> with JUnit 5.<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\" id=\"h-3-1-parent-pom\">3.1 Parent POM<\/h3>\n<p>Parent <strong>pom.xml<\/strong> includes three modules and two common build plug-ins:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>maven-compiler-plugin<\/code> &#8211; defines the Java 11 for the compiler<\/li>\n<li><code>maven-surefire-plugin<\/code> &#8211; defines the JUnit report plug-in<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span>\n<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\n\txmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\n\t&lt;groupId&gt;jcg.zheng.demo&lt;\/groupId&gt;\n\t&lt;artifactId&gt;junit-demo&lt;\/artifactId&gt;\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;packaging&gt;pom&lt;\/packaging&gt;\n\n\t&lt;name&gt;junit-demo&lt;\/name&gt;\n\t&lt;url&gt;http:\/\/maven.apache.org&lt;\/url&gt;\n\n\t&lt;properties&gt;\n\t\t&lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\n\t&lt;\/properties&gt;\n\n\n\t&lt;description&gt;parent project for junit demo&lt;\/description&gt;\n\t&lt;modules&gt;\n\t\t&lt;module&gt;common&lt;\/module&gt;\n\t\t&lt;module&gt;junit4-demo&lt;\/module&gt;\n\t\t&lt;module&gt;junit5-demo&lt;\/module&gt;\n\t&lt;\/modules&gt;\n\n\t&lt;build&gt;\n\t\t&lt;plugins&gt;\n\t\t\t&lt;plugin&gt;\n\t\t\t\t&lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\n\t\t\t\t&lt;version&gt;3.8.0&lt;\/version&gt;\n\t\t\t\t&lt;configuration&gt;\n\t\t\t\t\t&lt;release&gt;11&lt;\/release&gt;\n\t\t\t\t&lt;\/configuration&gt;\n\t\t\t&lt;\/plugin&gt;\n\t\t\t&lt;plugin&gt;\n\t\t\t\t&lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\n\t\t\t\t&lt;artifactId&gt;maven-surefire-plugin&lt;\/artifactId&gt;\n\t\t\t\t&lt;version&gt;3.0.0-M3&lt;\/version&gt;\n\t\t\t&lt;\/plugin&gt;\n\t\t&lt;\/plugins&gt;\n\t&lt;\/build&gt;\n\n&lt;\/project&gt;<\/pre>\n<p>Execute mvn clean install command and capture the output here:<\/p>\n<pre class=\"wp-block-preformatted brush:plain\">[INFO] Reactor Summary for junit-demo 0.0.1-SNAPSHOT:\n[INFO]\n[INFO] junit-demo ......................................... SUCCESS [  2.287 s]\n[INFO] comon .............................................. SUCCESS [ 10.295 s]\n[INFO] junit4-demo ........................................ SUCCESS [  6.631 s]\n[INFO] junit5-demo ........................................ SUCCESS [  6.191 s]\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS\n[INFO] ------------------------------------------------------------------------\n[INFO] Total time:  26.052 s\n[INFO] Finished at: 2020-03-30T20:46:54-05:00\n[INFO] ------------------------------------------------------------------------<\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-4-common-module\">4. Common Module<\/h2>\n<p>In this step, I will create a common module which contains a main class. The main class will be tested at both JUnit 4 and JUnit 5 at its respective module.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-4-1-pom\">4.1 POM<\/h3>\n<p>The <code>common<\/code> module&#8217;s <strong>pom.xml<\/strong> is defined as the following:<\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span>\n<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;?xml version=\"1.0\"?&gt;\n&lt;project\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"\n\txmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\n\txmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\t&lt;parent&gt;\n\t\t&lt;groupId&gt;jcg.zheng.demo&lt;\/groupId&gt;\n\t\t&lt;artifactId&gt;junit-demo&lt;\/artifactId&gt;\n\t\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;\/parent&gt;\n\n\t&lt;artifactId&gt;common&lt;\/artifactId&gt;\n\n\t&lt;name&gt;comon&lt;\/name&gt;\n\t&lt;url&gt;http:\/\/maven.apache.org&lt;\/url&gt;\n\n&lt;\/project&gt;\n<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-4-2-someclass\">4.2 SomeClass<\/h3>\n<p>In this step, I will create <code>SomeClass<\/code> which has the following methods:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>doubleANumber<\/code> &#8211; return an integer number by multiplying two.<\/li>\n<li><code>returnABoolean<\/code> &#8211; return a <code>boolean<\/code> value based on the input string value.<\/li>\n<li><code>voidFoo<\/code> &#8211; does not return anything and throws an exception when receiving a bad argument.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><em>SomeClass.java<\/em><\/span> <div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\npublic class SomeClass {\n\t\n \n\tpublic int doubleANumber(int num) {\n\t\treturn num * 2;\n\t}\n\t\n\tpublic boolean returnABoolean(String inputData) {\n\t\tif (\"Save\".equalsIgnoreCase(inputData)) {\n\t\t\treturn true;\n\t\t} else {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tpublic void voidFoo(String inputData) {\n\t\tif (\"Ok\".equalsIgnoreCase(inputData)) {\n\t\t\tSystem.out.println(\"doing something.\");;\n\t\t} else {\n\t\t\tthrow new IllegalArgumentException(\"Bad argument:\" + inputData);\n\t\t}\n\t}\n}\n<\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-5-junit-4-module\">5. JUnit 4 Module<\/h2>\n<p>JUnit 4 was first released in 2006. It only has one jar and requires JDK 5 or higher version.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-5-1-pom\">5.1 POM<\/h3>\n<p>The <code>JUnit4-demo<\/code> module&#8217;s <code>pom.xml<\/code> and depends on JUnit 4 and the <code>common<\/code> module. <\/p>\n<p>In this step, I will create a JUnit 4 test class to test <code>SomeClass<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span>\n<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;?xml version=\"1.0\"?&gt;\n&lt;project\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"\n\txmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\n\txmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\t&lt;parent&gt;\n\t\t&lt;groupId&gt;jcg.zheng.demo&lt;\/groupId&gt;\n\t\t&lt;artifactId&gt;junit-demo&lt;\/artifactId&gt;\n\t\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;\/parent&gt;\n\n\t&lt;artifactId&gt;junit4-demo&lt;\/artifactId&gt;\n\n\t&lt;name&gt;junit4-demo&lt;\/name&gt;\n\t&lt;url&gt;http:\/\/maven.apache.org&lt;\/url&gt;\n\t&lt;properties&gt;\n\t\t&lt;junit.version&gt;4.12&lt;\/junit.version&gt;\n\t&lt;\/properties&gt;\n\n\t&lt;dependencies&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;jcg.zheng.demo&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;common&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;junit&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;junit&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;${junit.version}&lt;\/version&gt;\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\n\t\t&lt;\/dependency&gt;\n\t&lt;\/dependencies&gt;\n\n&lt;\/project&gt;\n<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-5-2-someclasstest\">5.2 SomeClassTest<\/h3>\n<p>In this step, I will create a <code>SomeClassTest<\/code> class in JUnit 4.<\/p>\n<ul class=\"wp-block-list\">\n<li>Define a test with <code>@org.junit.Test<\/code><\/li>\n<li>Print out a test name with a <code>@org.junit.Rule<\/code> on a <code>org.junit.rules.TestName<\/code> class<\/li>\n<li>Setup the test before each tests with <code>@org.junit.Before<\/code><\/li>\n<li>Ignore a test with <code>@org.junit.Ignore<\/code><\/li>\n<li>Set a test with a timeout limitation<\/li>\n<li>Set a test with an expected exception<\/li>\n<li>Verify the testing result with the expected value with a static class <code>org.junit.Assert<\/code><\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><em>SomeClassTest.java<\/em><\/span> <\/p>\n<pre class=\"wp-block-preformatted brush:java; highlight:[20,23,26,29,31,34,42,48,60,65]\">package jcg.zheng.demo.junit4;\n\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertFalse;\nimport static org.junit.Assert.assertTrue;\nimport static org.junit.Assert.fail;\n\nimport org.junit.Before;\nimport org.junit.Ignore;\nimport org.junit.Rule;\nimport org.junit.Test;\nimport org.junit.rules.TestName;\n\nimport jcg.zheng.demo.SomeClass;\n\npublic class SomeClassTest {\n\n\tprivate SomeClass classUnderTest = new SomeClass();\n\t\n\t@Rule\n\tpublic TestName testName = new TestName();\n\n\t@Before\n\tpublic void setup() {\n\t\tclassUnderTest = new SomeClass();\n\t\tSystem.out.println(\"Start \" + testName.getMethodName());\n\t}\n\n\t@Test\n\tpublic void test_doubleANumber() {\n\t\tassertEquals(6, classUnderTest.doubleANumber(3));\n\t}\n\n\t@Ignore\n\tpublic void test_not_executed() {\n\t\tfail(\"It should not executed\");\n\t}\n\n\t@Test\n\tpublic void test_returnBooleanFoo_false() {\n\t\tboolean shouldReturnFalse = classUnderTest.returnABoolean(\"NA\");\n\t\tassertFalse(shouldReturnFalse);\n\t}\n\n\t@Test\n\tpublic void test_returnBooleanFoo_true() {\n\t\tboolean shouldReturnTrue = classUnderTest.returnABoolean(\"Save\");\n\t\tassertTrue(shouldReturnTrue);\n\t}\n\n\t@Test\n\tpublic void test_voidFoo() throws IllegalAccessException {\n\t\ttry {\n\t\t\tclassUnderTest.voidFoo(\"OK\");\n\t\t} catch (Exception e) {\n\t\t\tfail(\"Should not throw exception\");\n\t\t}\n\t}\n\n\t@Test(expected = IllegalArgumentException.class)\n\tpublic void test_voidFoo_exception() throws IllegalAccessException {\n\t\tclassUnderTest.voidFoo(\"NA\");\n\t}\n\t\n\t@Test(timeout = 1)\n\tpublic void test_timeout() {\n\t\tclassUnderTest.doubleANumber(9999);\n\t}\n}\n<\/pre>\n<ul class=\"wp-block-list\">\n<li>Line 20, 26 &#8211; the <code>TestName<\/code> instance marked by <code>@Rule<\/code> can access the test name.<\/li>\n<li>Line 23 &#8211;  the method marked with <code>@Before<\/code> will be invoked before executing each test.<\/li>\n<li>Line 29 &#8211; <code>@Test<\/code> marks a method as a test. It will be executed by the JUnit default runner. <\/li>\n<li>Line 34 &#8211; JUnit runner will ignore test tests which marks with <code>@Ignore<\/code>.<\/li>\n<li>Line 31, 42, 48 &#8211; invokes <code>assertFalse<\/code>, <code>assertTrue<\/code>, <code>assertEquals<\/code> to verify the test results to the expected value.<\/li>\n<li>Line 60 &#8211; catch the expected exception.<\/li>\n<li>Line 65 &#8211; set up the timeout limit.<\/li>\n<\/ul>\n<p><em><span style=\"text-decoration: underline;\">Output<\/span><\/em> <\/p>\n<pre class=\"wp-block-preformatted brush:plain\">[INFO] -------------------------------------------------------\n[INFO]  T E S T S\n[INFO] -------------------------------------------------------\n[INFO] Running jcg.zheng.demo.junit4.SomeClassTest\nStart test_voidFoo\ndoing something.\nStart test_returnBooleanFoo_false\nStart test_voidFoo_exception\nStart test_doubleANumber\nStart test_timeout\nStart test_returnBooleanFoo_true\n[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.214 s - in jcg.zheng.demo.junit4.SomeClassTest\n[INFO]\n[INFO] Results:\n[INFO]\n[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0<\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-6-junit-5-module\">6. JUnit 5 Module<\/h2>\n<p>JUnit 5 was first released in 2017. It requires JDK 8 or higher. It includes a collection of three sub-projects: JUnit Jupiter, JUnit Platform, and JUnit Vintage. <\/p>\n<h3 class=\"wp-block-heading\" id=\"h-6-1-pom\">6.1 POM<\/h3>\n<p>The <code>JUnit5-demo<\/code> module&#8217;s <code>pom.xml<\/code>&nbsp;depends on JUnit 5 and common modules. Please note that it includes two of JUnit 5 modules: <code>junit-jupiter-engine <\/code>and <code>junit-jupiter-api<\/code>.<\/p>\n<p>In this step, I will create a JUnit 5 test class to test <code>SomeClass<\/code>. <\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span>\n<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;?xml version=\"1.0\"?&gt;\n&lt;project\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"\n\txmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\n\txmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\t&lt;parent&gt;\n\t\t&lt;groupId&gt;jcg.zheng.demo&lt;\/groupId&gt;\n\t\t&lt;artifactId&gt;junit-demo&lt;\/artifactId&gt;\n\t\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;\/parent&gt;\n\n\t&lt;artifactId&gt;junit5-demo&lt;\/artifactId&gt;\n\n\t&lt;name&gt;junit5-demo&lt;\/name&gt;\n\t&lt;url&gt;http:\/\/maven.apache.org&lt;\/url&gt;\n\t&lt;properties&gt;\n\t\t&lt;junit-jupiter.version&gt;5.5.2&lt;\/junit-jupiter.version&gt;\n\t&lt;\/properties&gt;\n\n\t&lt;dependencies&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;jcg.zheng.demo&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;common&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t \n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.junit.jupiter&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;junit-jupiter-engine&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;${junit-jupiter.version}&lt;\/version&gt;\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\n\t\t&lt;\/dependency&gt;\n\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.junit.jupiter&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;junit-jupiter-api&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;${junit-jupiter.version}&lt;\/version&gt;\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\n\t\t&lt;\/dependency&gt;\n\n\t&lt;\/dependencies&gt;\n\n\n&lt;\/project&gt;\n<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-6-2-someclasstest\">6.2 SomeClassTest<\/h3>\n<p>In this step, I will create a <code>SomeClassTest<\/code> class in JUnit 5.<\/p>\n<ul class=\"wp-block-list\">\n<li>Define a test with <code>@org.junit.jupiter.api.Test<\/code><\/li>\n<li>Define a display name with <code>@org.junit.jupiter.api.DisplayName<\/code><\/li>\n<li>Print out a test name from <code>@org.junit.jupiter.api.TestInfo<\/code><\/li>\n<li>Setup the test before each tests with <code>@org.junit.jupiter.api.BeforeEach<\/code><\/li>\n<li>Ignore a test with <code>@org.junit.jupiter.api.Disabled<\/code><\/li>\n<li>Set a test with the <code>org.junit.jupiter.api.assertTimeout<\/code> method<\/li>\n<li>Catch an exception with the <code>org.junit.jupiter.api.assertThrow<\/code> method<\/li>\n<li>Verify the testing result with the expected value with a <code>static<\/code> class: <code>org.junit.jupiter.api.Assertions<\/code><\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><em>SomeClassTest.java<\/em><\/span> <\/p>\n<pre class=\"wp-block-preformatted brush:java highlight:[28,29,35,40,45,46,72,80]\">package jcg.zheng.demo.junit5;\n\nimport static org.junit.jupiter.api.Assertions.assertEquals;\nimport static org.junit.jupiter.api.Assertions.assertFalse;\nimport static org.junit.jupiter.api.Assertions.assertThrows;\nimport static org.junit.jupiter.api.Assertions.assertTimeout;\nimport static org.junit.jupiter.api.Assertions.assertTrue;\nimport static org.junit.jupiter.api.Assertions.fail;\n\nimport java.time.Duration;\n\nimport org.junit.jupiter.api.BeforeEach;\nimport org.junit.jupiter.api.Disabled;\nimport org.junit.jupiter.api.DisplayName;\nimport org.junit.jupiter.api.RepeatedTest;\nimport org.junit.jupiter.api.Test;\nimport org.junit.jupiter.api.TestInfo;\nimport org.junit.jupiter.api.TestReporter;\n\nimport jcg.zheng.demo.SomeClass;\n\npublic class SomeClassTest {\n\n\tprivate SomeClass classUnderTest;\n\tprivate TestInfo testInfo;\n\tprivate TestReporter testReporter;\n\n\t@BeforeEach\n\tpublic void setup(TestInfo testInfo, TestReporter terstReporter ) {\n\t\tthis.testInfo = testInfo;\n\t\tthis.testReporter = terstReporter;\n\t\tclassUnderTest = new SomeClass();\n\t}\n\n\t@RepeatedTest(5)\n\tpublic void test_doubleANumber() {\n\t\tassertEquals(6, classUnderTest.doubleANumber(3), \"it should return 6\");\n\t}\n\n\t@Disabled\n\tpublic void test_not_executed() {\n\t\tfail(\"It should not executed\");\n\t}\n\n\t@Test\n\t@DisplayName(\"It should return false when input data isn't Save\")\n\tpublic void test_returnBooleanFoo_false() {\n\t\tboolean shouldReturnFalse = classUnderTest.returnABoolean(\"NA\");\n\t\tassertFalse(shouldReturnFalse);\n\t}\n\n\t@Test\n\t@DisplayName(\"It should return true when input data is Save\")\n\tpublic void test_returnBooleanFoo_true() {\n\t\tboolean shouldReturnTrue = classUnderTest.returnABoolean(\"Save\");\n\t\tassertTrue(shouldReturnTrue);\n\t\ttestReporter.publishEntry(testInfo.getDisplayName());\n\t}\n\n\t@Test\n\tpublic void test_voidFoo() throws IllegalAccessException {\n\t \n\t\ttry {\n\t\t\tclassUnderTest.voidFoo(\"OK\");\n\t\t} catch (Exception e) {\n\t\t\tfail(\"Should not throw exception\");\n\t\t}\n\t}\n\n\t@Test\n\tpublic void test_voidFoo_exception() throws IllegalAccessException {\n\t\tassertThrows(IllegalArgumentException.class, () -&gt; {\n\t\t\tclassUnderTest.voidFoo(\"NA\");\n\t\t});\n\n\t}\n\t\n\t@Test\n\tpublic void test_timeout() {\n\t\tassertTimeout(Duration.ofMillis(1), ()-&gt; classUnderTest.doubleANumber(1000));\n\t}\n\n}\n<\/pre>\n<ul class=\"wp-block-list\">\n<li>Line 28 &#8211; <code>@BeforeEach<\/code> marks the method to be executed for each test.<\/li>\n<li>Line 29 &#8211; can inject <code>TestInfo<\/code> and <code>TestReporter<\/code> from Junit framework.<\/li>\n<li>Line 35 &#8211; <code>@RepeatedTest<\/code> annotation is a new annotation in Junit 5 which executes the test repeatedly.<\/li>\n<li>Line 40 &#8211; <code>@Disabled<\/code> annotation replaces the <code>@Ignore<\/code> annotation in  Junit 4.<\/li>\n<li>Line 45 &#8211; <code>@Test<\/code> in <code>org.junit.jupiter.api<\/code> package marks a test. <\/li>\n<li>Line 46 &#8211; <code>@DisplayName<\/code> is a new annotation which names the test with a more meaningful name.<\/li>\n<li>Line 72 &#8211; <code>assertThrows<\/code> in JUnit 5 replaces the <code>@Test<\/code>&#8216;s  <code>expected<\/code> attribute in Junit 4 .<\/li>\n<li>Line 80 &#8211; <code>assertTimeout<\/code> in JUnit 5 replaces the <code>@Test<\/code>&#8216;s <code>timeout<\/code> attribute in Junit 4 . <\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span>\n<\/p>\n<pre class=\"wp-block-preformatted brush:plain\">[INFO] -------------------------------------------------------\n[INFO]  T E S T S\n[INFO] -------------------------------------------------------\n[INFO] Running jcg.zheng.demo.junit5.SomeClassTest\ndoing something.\n[INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.254 s - in jcg.zheng.demo.junit5.SomeClassTest\n[INFO]\n[INFO] Results:\n[INFO]\n[INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0<\/pre>\n<p>In Eclipse IDE, you can see the test with the display name.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"800\" height=\"400\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/03\/junit5-report.jpg\" alt=\"JUnit Example - JUnit 5 Result\" class=\"wp-image-87496\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/03\/junit5-report.jpg 800w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/03\/junit5-report-300x150.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/03\/junit5-report-768x384.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><figcaption>Figure 1 JUnit 5 Result<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\" id=\"h-7-summary\">7. Summary<\/h2>\n<p>In this example, I demonstrated how to write a unit test in JUnit. Here are the major differences between JUnit 4 and JUnit 5:<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>JUnit 4<\/strong><\/td>\n<td><strong>JUnit 5<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>Required JDK<\/strong><\/td>\n<td>5 (+)<\/td>\n<td>8 (+)<\/td>\n<\/tr>\n<tr>\n<td><strong>Package<\/strong><\/td>\n<td><code>org.junit<\/code><\/td>\n<td><code>org.junit.jupiter<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Annotation<\/strong><\/td>\n<td><code>@Before<\/code><\/td>\n<td><code>@BeforeEach<\/code><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>@After<\/code><\/td>\n<td><code>@AfterEach<\/code><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>@BeforeClass<\/code><\/td>\n<td><code>@BeforeAll<\/code><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>@AfterClass<\/code><\/td>\n<td><code>@AfterAll<\/code><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>@Ignore<\/code><\/td>\n<td><code>@Disabled<\/code><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>&#8211;<\/td>\n<td><code>@DisplayName<\/code><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>&#8211;<\/td>\n<td><code>@RepeatedTest<\/code><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>@Category<\/td>\n<td><code>@Tag<\/code><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>@RunWith<\/td>\n<td>@ExtendWith<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-8-download-the-source-code\">8. Download the Source Code <\/h2>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/03\/junit-demo.zip\"><strong>Java Unit Testing with JUnit Example<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is an article for Java Unit Testing with JUnit Example. You can also check this tutorial in the following video: JUnit Testing in Java &#8211; video 1. Introduction Java unit testing is a software testing where methods and classes are tested. JUnit is a unit testing framework for the Java programming language which provides &hellip;<\/p>\n","protected":false},"author":140,"featured_media":6678,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-87265","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-junit"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Unit Testing with JUnit Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This is an article for Java Unit Testing with JUnit Example. You can also check this tutorial in the following video: JUnit Testing in Java - video 1.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Unit Testing with JUnit Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This is an article for Java Unit Testing with JUnit Example. You can also check this tutorial in the following video: JUnit Testing in Java - video 1.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-03T08:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-07T12:05:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Mary Zheng\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mary Zheng\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/\"},\"author\":{\"name\":\"Mary Zheng\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/8a2034fbabcb20a9396e9819261855ae\"},\"headline\":\"Java Unit Testing with JUnit Example\",\"datePublished\":\"2020-04-03T08:00:00+00:00\",\"dateModified\":\"2022-07-07T12:05:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/\"},\"wordCount\":795,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"articleSection\":[\"junit\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/\",\"name\":\"Java Unit Testing with JUnit Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"datePublished\":\"2020-04-03T08:00:00+00:00\",\"dateModified\":\"2022-07-07T12:05:27+00:00\",\"description\":\"This is an article for Java Unit Testing with JUnit Example. You can also check this tutorial in the following video: JUnit Testing in Java - video 1.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"junit\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/junit\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Java Unit Testing with JUnit Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/8a2034fbabcb20a9396e9819261855ae\",\"name\":\"Mary Zheng\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/11\/Mary-Zheng_avatar_1510732235-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/11\/Mary-Zheng_avatar_1510732235-96x96.jpg\",\"caption\":\"Mary Zheng\"},\"description\":\"Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/mary-zheng\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Unit Testing with JUnit Example - Java Code Geeks","description":"This is an article for Java Unit Testing with JUnit Example. You can also check this tutorial in the following video: JUnit Testing in Java - video 1.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/","og_locale":"en_US","og_type":"article","og_title":"Java Unit Testing with JUnit Example - Java Code Geeks","og_description":"This is an article for Java Unit Testing with JUnit Example. You can also check this tutorial in the following video: JUnit Testing in Java - video 1.","og_url":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2020-04-03T08:00:00+00:00","article_modified_time":"2022-07-07T12:05:27+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","type":"image\/jpeg"}],"author":"Mary Zheng","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Mary Zheng","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/"},"author":{"name":"Mary Zheng","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/8a2034fbabcb20a9396e9819261855ae"},"headline":"Java Unit Testing with JUnit Example","datePublished":"2020-04-03T08:00:00+00:00","dateModified":"2022-07-07T12:05:27+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/"},"wordCount":795,"commentCount":1,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","articleSection":["junit"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/","name":"Java Unit Testing with JUnit Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","datePublished":"2020-04-03T08:00:00+00:00","dateModified":"2022-07-07T12:05:27+00:00","description":"This is an article for Java Unit Testing with JUnit Example. You can also check this tutorial in the following video: JUnit Testing in Java - video 1.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-junit-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"junit","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/junit\/"},{"@type":"ListItem","position":5,"name":"Java Unit Testing with JUnit Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/8a2034fbabcb20a9396e9819261855ae","name":"Mary Zheng","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/11\/Mary-Zheng_avatar_1510732235-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/11\/Mary-Zheng_avatar_1510732235-96x96.jpg","caption":"Mary Zheng"},"description":"Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.","url":"https:\/\/examples.javacodegeeks.com\/author\/mary-zheng\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/87265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/140"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=87265"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/87265\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/6678"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=87265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=87265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=87265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}