{"id":87646,"date":"2020-04-13T11:00:00","date_gmt":"2020-04-13T08:00:00","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=87646"},"modified":"2020-04-08T12:50:07","modified_gmt":"2020-04-08T09:50:07","slug":"java-unit-testing-with-mockito-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/","title":{"rendered":"Java Unit Testing with Mockito Example"},"content":{"rendered":"<p>In this post, we feature a comprehensive article about Java Unit Testing with Mockito Example.<\/p>\n<h2 class=\"wp-block-heading\">1. Introduction<\/h2>\n<p>Java unit testing is a software testing which focuses on testing methods in a class. When a class depends on other classes and\/or interfaces, we can test it by using <a rel=\"noreferrer noopener\" href=\"https:\/\/site.mockito.org\/\" target=\"_blank\">Mockito<\/a> to create and configure mock objects.<\/p>\n<p>In this example, first, I will create a class which depends on an interface and other class. Second, I will test it using Mockito with Junit framework. I will demonstrate the following actions:<\/p>\n<ol class=\"wp-block-list\">\n<li>Define a mocked object with <code>@Mock<\/code>.<\/li>\n<li>Define a test class which depends on mocked objects with <code>@InjectMocks<\/code>.<\/li>\n<li>Configure a mocked object&#8217;s behavior.<\/li>\n<li>Verify a mocked object&#8217;s behavior.<\/li>\n<\/ol>\n<h2 class=\"wp-block-heading\">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.12 and 5.5.2<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">3. Maven Project<\/h2>\n<p>In this step, I will demonstrate how to use Mockito in both JUnit 4 and JUnit 5 in a three-module Maven project:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>common<\/code> \u2013 includes four classes \u2013 <code>SomeClass<\/code>, <code>OtherService<\/code>, <code>SomeInterface<\/code>, and <code>SomeException<\/code>. <code>SomeClass<\/code> depends on <code>OtherService<\/code> and <code>SomeInterface<\/code>.<\/li>\n<li><code>JUnit4-demo<\/code>\u2013 tests&nbsp;<code>SomeClass<\/code>&nbsp;with Mockito in JUnit 4.<\/li>\n<li><code>JUnit5-demo<\/code> \u2013 tests <code>SomeClass<\/code> with Mockito in JUnit 5.<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\">3.1 Dependencies<\/h3>\n<p>Parent <strong>pom.xml<\/strong> includes three modules.<\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span> <\/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-M4&lt;\/version&gt;\n\t\t\t&lt;\/plugin&gt;\n\t\t\t &lt;plugin&gt;\n                &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\n                &lt;artifactId&gt;maven-site-plugin&lt;\/artifactId&gt;\n                &lt;version&gt;3.8.2&lt;\/version&gt;\n            &lt;\/plugin&gt;\n\t\t&lt;\/plugins&gt;\n\t&lt;\/build&gt;\n\n\t&lt;reporting&gt;\n\t\t&lt;plugins&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-report-plugin&lt;\/artifactId&gt;\n\t\t\t\t&lt;version&gt;3.0.0-M4&lt;\/version&gt;\n\t\t\t&lt;\/plugin&gt;\n\t\t&lt;\/plugins&gt;\n\t&lt;\/reporting&gt;\n&lt;\/project&gt;<\/pre>\n<h2 class=\"wp-block-heading\">4. Common Module<\/h2>\n<p>In this step, I will create a common module which contains four classes:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>OtherService<\/code> &#8211; a class which has three public methods.<\/li>\n<li><code>SomeInterface<\/code> &#8211; an interface which defines two public methods.<\/li>\n<li><code>SomeException<\/code> &#8211; a run time exception<\/li>\n<li><code>SomeClass<\/code> &#8211; has four methods and depends on both <code>OtherService<\/code> and <code>SomeInterface<\/code>.<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\">4.1 POM<\/h3>\n<p>The&nbsp;<code>common<\/code>&nbsp;module\u2019s&nbsp;<strong>pom.xml<\/strong>&nbsp;is defined as the following:<\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span> <\/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\">4.2 SomeInterface<\/h3>\n<p>In this step, I will create&nbsp;<code>SomeInterface<\/code>&nbsp;which has the following methods:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>doSomething<\/code> &#8211; it does not return anything.<\/li>\n<li><code>getSomeList<\/code> &#8211; it returns a list of string values.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><em>SomeInterface.java<\/em><\/span> <\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\nimport java.util.List;\n\npublic interface SomeInterface {\n\t\n\tvoid doSomething(String input);\n\t\n\tList&lt;String&gt; getSomeList(String input);\n\n}\n<\/pre>\n<h3 class=\"wp-block-heading\">4.3 OtherService<\/h3>\n<p>In this step, I will create&nbsp;<code>OtherService<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>OtherService.java<\/em><\/span> <\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\npublic class OtherService {\n\n\tpublic int calculateByDoubleIt(int num) {\n\t\treturn num * 2;\n\t}\n\t\n\tpublic void doSomething(String input) {\n\t\tif (\"Ok\".equalsIgnoreCase(input)) {\n\t\t\tSystem.out.println(\"do something.\");\n\t\t} else {\n\t\t\tthrow new RuntimeException(\"Remote service failed\");\n\t\t}\n\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<\/pre>\n<h3 class=\"wp-block-heading\">4.4 SomeException<\/h3>\n<p>In this step, I will create a <code>SomeException<\/code> class which extends from <code>RuntimeExcpetion<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>SomeException.java<\/em><\/span> <\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\npublic class SomeException extends RuntimeException {\n\n\tprivate static final long serialVersionUID = 347808963459470775L;\n\t\n\tpublic SomeException(String msg) {\n\t\tsuper(msg);\n\t}\n\n}\n<\/pre>\n<h3 class=\"wp-block-heading\">4.5 SomeClass<\/h3>\n<p>In this step, I will create a&nbsp;<code>SomeClass<\/code>&nbsp;which depends on both <code>SomeInterface<\/code> and <code>OtherService<\/code>. It has the following methods:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<ul class=\"wp-block-list\">\n<li><code>doubleANumber<\/code> &#8211; returns an integer after invoking <code>someService.calculateByDoubleIt<\/code>.<\/li>\n<li><code>processData<\/code> &#8211; returns <code>void<\/code>. It depends on <code>someInterface.getSomeList<\/code> and <code>someService.doSomething<\/code><\/li>\n<li><code>returnABoolean<\/code> &#8211; returns a <code>Boolean<\/code> value after calling <code>someService.returnABoolean<\/code><\/li>\n<li><code>voidFoo<\/code> &#8211; returns <code>void<\/code>. It depends on <code>someInterface.doSomething<\/code> and <code>someService.doSomething<\/code>.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><em>SomeClass.java<\/em><\/span> <\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\nimport java.util.List;\n\npublic class SomeClass {\n\n\tprivate SomeInterface someInterface;\n\n\tprivate OtherService someService;\n\n\tpublic int doubleANumber(int num) {\n\t\ttry {\n\t\t\treturn someService.calculateByDoubleIt(num);\n\t\t} catch (Exception e) {\n\t\t\tthrow new SomeException(\"External Service-calculateByDoubleIt failed \" + e.getMessage());\n\t\t}\n\t}\n\n\tpublic void processData(String input) {\n\t\tList&lt;String&gt; rawList = someInterface.getSomeList(input);\n\t\tif (rawList != null) {\n\t\t\tfor (String item : rawList) {\n\t\t\t\tsomeService.doSomething(item);\n\t\t\t}\n\t\t}\n\n\t}\n\n\tpublic boolean returnABoolean(String inputData) {\n\t\ttry {\n\t\t\treturn someService.returnABoolean(inputData);\n\t\t} catch (Exception e) {\n\t\t\tthrow new SomeException(\"External Service-returnABoolean failed \" + e.getMessage());\n\t\t}\n\t}\n\n\tpublic void voidFoo(String inputData) {\n\t\ttry {\n\t\t\tif (\"Interface\".equalsIgnoreCase(inputData)) {\n\t\t\t\tsomeInterface.doSomething(inputData);\n\t\t\t}\n\t\t\tsomeService.doSomething(inputData);\n\t\t} catch (Exception e) {\n\t\t\tthrow new SomeException(\"External Service-doSomething failed \" + e.getMessage());\n\t\t}\n\t}\n}\n<\/pre>\n<h2 class=\"wp-block-heading\">5. JUnit 4 Test<\/h2>\n<p>In this step, I will create a JUnit 4 test class to test&nbsp;<code>SomeClass<\/code> with mocked <code>SomeInterface<\/code> and <code>OtherService<\/code>.<\/p>\n<h3 class=\"wp-block-heading\">5.1 POM<\/h3>\n<p>The&nbsp;<code>JUnit4-demo<\/code>&nbsp;module\u2019s&nbsp;<code>pom.xml<\/code>&nbsp;and depends on <code>mockito-core<\/code> and the&nbsp;<code>common<\/code>&nbsp;module.<\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span> <\/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\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\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.mockito&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;mockito-core&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;3.2.4&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\">5.2 SomeClassTest<\/h3>\n<p>In this step, I will create a&nbsp;<code>SomeClassTest<\/code>&nbsp;class in JUnit 4. It will use the following Mockito annotation and libraries:<\/p>\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/javadoc.io\/doc\/org.mockito\/mockito-core\/latest\/org\/mockito\/Mock.html\" target=\"_blank\">@Mock<\/a> &#8211; marks a field as a mock object.<\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/javadoc.io\/doc\/org.mockito\/mockito-core\/latest\/org\/mockito\/InjectMocks.html\" target=\"_blank\">@InjectMocks<\/a> &#8211; marks a field on which injection should be performed.<\/li>\n<li>@RunWith(<a rel=\"noreferrer noopener\" href=\"https:\/\/www.javadoc.io\/doc\/org.mockito\/mockito-core\/2.2.28\/org\/mockito\/junit\/MockitoJUnitRunner.html\" target=\"_blank\">MockitoJUnitRunner.class<\/a>) &#8211; Integrates with Junit 4 runner<\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/javadoc.io\/static\/org.mockito\/mockito-core\/3.3.3\/org\/mockito\/Mockito.html\" target=\"_blank\">Mockito <\/a>static methods: <code>when<\/code>, <code>doThrow<\/code>, <code>times<\/code>, and <code>verify<\/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\">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;\nimport static org.mockito.ArgumentMatchers.anyString;\nimport static org.mockito.Mockito.doThrow;\nimport static org.mockito.Mockito.times;\nimport static org.mockito.Mockito.verify;\nimport static org.mockito.Mockito.when;\n\nimport java.util.Arrays;\nimport java.util.List;\n\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.mockito.InjectMocks;\nimport org.mockito.Mock;\nimport org.mockito.junit.MockitoJUnitRunner;\n\nimport jcg.zheng.demo.SomeException;\nimport jcg.zheng.demo.SomeClass;\nimport jcg.zheng.demo.OtherService;\nimport jcg.zheng.demo.SomeInterface;\n\n@RunWith(MockitoJUnitRunner.class)\npublic class SomeClassTest {\n\n\t@InjectMocks\n\tprivate SomeClass classUnderTest;\n\t\n\t@Mock\n\tprivate SomeInterface mockedInterface;\n\n\t@Mock\n\tprivate OtherService mockedService;\n\n\t@Test\n\tpublic void test_doubleANumber() {\n\t\twhen(mockedService.calculateByDoubleIt(3)).thenReturn(6);\n\n\t\tassertEquals(6, classUnderTest.doubleANumber(3));\n\t\tverify(mockedService, times(1)).calculateByDoubleIt(3);\n\t}\n\n\t@Test(expected = SomeException.class)\n\tpublic void test_doubleANumber_exception() {\n\t\twhen(mockedService.calculateByDoubleIt(3)).thenThrow(NullPointerException.class);\n\t\tclassUnderTest.doubleANumber(3);\n\n\t\tverify(mockedService, times(1)).calculateByDoubleIt(3);\n\t}\n\n\t@Test\n\tpublic void test_processData() {\n\t\twhen(mockedInterface.getSomeList(anyString())).thenReturn(null);\n\t\tclassUnderTest.processData(\"NA\");\n\t\t\n\t\tverify(mockedInterface, times(1)).getSomeList(anyString());\n\t\tverify(mockedService, times(0)).doSomething(anyString());\n\t}\n\n\t@Test\n\tpublic void test_processData_2() {\n\t\tList&lt;String&gt; twoItemsList = Arrays.asList(\"Mary\", \"Zheng\");\n\t\twhen(mockedInterface.getSomeList(anyString())).thenReturn(twoItemsList);\n\t\tclassUnderTest.processData(\"NA\");\n\t\t\n\t\tverify(mockedInterface, times(1)).getSomeList(anyString());\n\t\tverify(mockedService, times(2)).doSomething(anyString());\n\t}\n\n\t@Test(expected = SomeException.class)\n\tpublic void test_returnBooleanFoo_exception() {\n\t\twhen(mockedService.returnABoolean(\"NA\")).thenThrow(NullPointerException.class);\n\t\tclassUnderTest.returnABoolean(\"NA\");\n\n\t\tverify(mockedService, times(1)).returnABoolean(\"NA\");\n\t}\n\n\t@Test\n\tpublic void test_returnBooleanFoo_false() {\n\t\twhen(mockedService.returnABoolean(\"NA\")).thenReturn(false);\n\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\twhen(mockedService.returnABoolean(\"Save\")).thenReturn(true);\n\n\t\tboolean shouldReturnTrue = classUnderTest.returnABoolean(\"Save\");\n\t\tassertTrue(shouldReturnTrue);\n\t\tverify(mockedService, times(1)).returnABoolean(\"Save\");\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\tverify(mockedService, times(1)).doSomething(\"Ok\");\n\t\t\tverify(mockedInterface, times(0)).doSomething(anyString());\n\t\t} catch (Exception e) {\n\t\t\tfail(\"Should not throw exception\");\n\t\t}\n\t}\n\n\t\n\t@Test(expected = SomeException.class)\n\tpublic void test_voidFoo_exception() {\n\t\tdoThrow(IllegalStateException.class).when(mockedService).doSomething(\"NA\");\n\t\tclassUnderTest.voidFoo(\"NA\");\n\t}\n\t\n\t@Test\n\tpublic void test_voidFoo_interface() throws IllegalAccessException {\n\t\ttry {\n\t\t\tclassUnderTest.voidFoo(\"Interface\");\n\t\t\tverify(mockedService, times(1)).doSomething(\"Interface\");\n\t\t\tverify(mockedInterface, times(1)).doSomething(\"Interface\");\n\t\t} catch (Exception e) {\n\t\t\tfail(\"Should not throw exception\");\n\t\t}\n\t}\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span>\n[ulp id=&#8217;GhnY46zldrhgoeym&#8217;]<\/p>\n<pre class=\"wp-block-preformatted brush:plain\">[INFO] Running jcg.zheng.demo.junit4.SomeClassTest\n[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.43 s - in jcg.zheng.demo.junit4.SomeClassTest\n[INFO]\n[INFO] Results:\n[INFO]\n[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0<\/pre>\n<h2 class=\"wp-block-heading\">6. JUnit 5 Module<\/h2>\n<p>In this step, I will create a JUnit 5 test class to test&nbsp;<code>SomeClass<\/code> with mocked objects. <\/p>\n<h3 class=\"wp-block-heading\">6.1 POM<\/h3>\n<p>The&nbsp;<code>JUnit5-demo<\/code>&nbsp;module\u2019s&nbsp;<code>pom.xml<\/code>&nbsp;depends on JUnit 5 and common modules. Please note that it includes:&nbsp;<code>junit-jupiter-engine&nbsp;<\/code>,<code>junit-jupiter-api<\/code>, <code>mockito-core<\/code>, and <code>mockito-junit-jupiter<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span> <\/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\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\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.junit.platform&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;junit-platform-runner&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;1.5.2&lt;\/version&gt;\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.mockito&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;mockito-core&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;3.2.4&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.mockito&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;mockito-junit-jupiter&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;2.23.0&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\">6.2 SomeClassTest<\/h3>\n<p>In this step, I will create a&nbsp;<code>SomeClassTest<\/code>&nbsp;class in JUnit 5. It has the same annotations as Junit 4, the only difference is Junit 5 uses <code>@ExtendWith(MockitoExtension.class)<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>SomeClassTest.java<\/em><\/span> <\/p>\n<pre class=\"wp-block-preformatted brush:java\">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.assertTrue;\nimport static org.mockito.ArgumentMatchers.anyString;\nimport static org.mockito.Mockito.doThrow;\nimport static org.mockito.Mockito.times;\nimport static org.mockito.Mockito.verify;\nimport static org.mockito.Mockito.when;\n\nimport java.util.Arrays;\nimport java.util.List;\n\nimport org.junit.jupiter.api.DisplayName;\nimport org.junit.jupiter.api.Test;\nimport org.junit.jupiter.api.extension.ExtendWith;\nimport org.mockito.InjectMocks;\nimport org.mockito.Mock;\nimport org.mockito.junit.jupiter.MockitoExtension;\n\nimport jcg.zheng.demo.SomeException;\nimport jcg.zheng.demo.SomeClass;\nimport jcg.zheng.demo.OtherService;\nimport jcg.zheng.demo.SomeInterface;\n\n@ExtendWith(MockitoExtension.class)\npublic class SomeClassTest {\n\n\t@InjectMocks\n\tprivate SomeClass classUnderTest;\n\n\t@Mock\n\tprivate SomeInterface mockedInterface;\n\n\t@Mock\n\tprivate OtherService mockedService;\n\n\t@Test\n\t@DisplayName(\"It should double a number\")\n\tpublic void test_doubleANumber() {\n\t\twhen(mockedService.calculateByDoubleIt(3)).thenReturn(6);\n\n\t\tassertEquals(6, classUnderTest.doubleANumber(3));\n\t\tverify(mockedService, times(1)).calculateByDoubleIt(3);\n\t}\n\n\t@Test\n\tpublic void test_doubleANumber_exception() {\n\t\twhen(mockedService.calculateByDoubleIt(3)).thenThrow(NullPointerException.class);\n\n\t\tassertThrows(SomeException.class, () -&gt; {\n\t\t\tclassUnderTest.doubleANumber(3);\n\t\t});\n\n\t\tverify(mockedService, times(1)).calculateByDoubleIt(3);\n\t}\n\n\t@Test\n\tpublic void test_processData() {\n\t\twhen(mockedInterface.getSomeList(anyString())).thenReturn(null);\n\t\tclassUnderTest.processData(\"NA\");\n\n\t\tverify(mockedInterface, times(1)).getSomeList(anyString());\n\t\tverify(mockedService, times(0)).doSomething(anyString());\n\t}\n\n\t@Test\n\tpublic void test_processData_2() {\n\t\tList&lt;String&gt; twoItemsList = Arrays.asList(\"Mary\", \"Zheng\");\n\t\twhen(mockedInterface.getSomeList(anyString())).thenReturn(twoItemsList);\n\t\tclassUnderTest.processData(\"NA\");\n\n\t\tverify(mockedInterface, times(1)).getSomeList(anyString());\n\t\tverify(mockedService, times(2)).doSomething(anyString());\n\t}\n\n\t@Test\n\tpublic void test_returnBooleanFoo_exception() {\n\t\twhen(mockedService.returnABoolean(\"NA\")).thenThrow(NullPointerException.class);\n\t\tassertThrows(SomeException.class, () -&gt; {\n\t\t\tclassUnderTest.returnABoolean(\"NA\");\n\t\t});\n\n\t\tverify(mockedService, times(1)).returnABoolean(\"NA\");\n\t}\n\n\t@Test\n\tpublic void test_returnBooleanFoo_false() {\n\t\twhen(mockedService.returnABoolean(\"NA\")).thenReturn(false);\n\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\twhen(mockedService.returnABoolean(\"Save\")).thenReturn(true);\n\n\t\tboolean shouldReturnTrue = classUnderTest.returnABoolean(\"Save\");\n\t\tassertTrue(shouldReturnTrue);\n\t\tverify(mockedService, times(1)).returnABoolean(\"Save\");\n\t}\n\n\t@Test\n\tpublic void test_voidFoo() throws IllegalAccessException {\n\n\t\tclassUnderTest.voidFoo(\"OK\");\n\t\tverify(mockedService, times(1)).doSomething(\"OK\");\n\n\t}\n\n\t@Test\n\tpublic void test_voidFoo_exception() {\n\t\tdoThrow(IllegalStateException.class).when(mockedService).doSomething(\"NA\");\n\t\tassertThrows(SomeException.class, () -&gt; {\n\t\t\tclassUnderTest.voidFoo(\"NA\");\n\t\t});\n\n\t}\n\n\t@Test\n\tpublic void test_voidFoo_interface() throws IllegalAccessException {\n\n\t\tclassUnderTest.voidFoo(\"Interface\");\n\t\tverify(mockedService, times(1)).doSomething(\"Interface\");\n\t\tverify(mockedInterface, times(1)).doSomething(\"Interface\");\n\n\t}\n\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span>\n<\/p>\n<pre class=\"wp-block-preformatted brush:plain\">[INFO] Running jcg.zheng.demo.junit5.SomeClassTest\n[INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.042 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<h2 class=\"wp-block-heading\">7. Summary<\/h2>\n<p>In this example, I demonstrated how to use Mockito to <\/p>\n<ul class=\"wp-block-list\">\n<li>Create mocked objects with <code>@Mock<\/code>.<\/li>\n<li>Inject the mocked objects for the testing class marked with <code>@InjectMocks<\/code>.<\/li>\n<li>Use Mockito <code>static<\/code> method to mock the object&#8217;s behavior.<\/li>\n<li>Verify the mocked object&#8217;s method is invoked.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">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\/04\/junit-mockito-demo.zip\"><strong>Java Unit Testing With Mokito Example<\/strong><\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we feature a comprehensive article about Java Unit Testing with Mockito Example. 1. Introduction Java unit testing is a software testing which focuses on testing methods in a class. When a class depends on other classes and\/or interfaces, we can test it by using Mockito to create and configure mock objects. In &hellip;<\/p>\n","protected":false},"author":140,"featured_media":21338,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[923],"tags":[],"class_list":["post-87646","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mockito"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unit Testing with Mockito Example - Examples Java Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"In this post, we feature a comprehensive article about Java Unit Testing with Mockito Example. 1. Introduction Java unit testing is a software testing\" \/>\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-mockito-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unit Testing with Mockito Example - Examples Java Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"In this post, we feature a comprehensive article about Java Unit Testing with Mockito Example. 1. Introduction Java unit testing is a software testing\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-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-13T08:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-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=\"13 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-mockito-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/\"},\"author\":{\"name\":\"Mary Zheng\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/8a2034fbabcb20a9396e9819261855ae\"},\"headline\":\"Java Unit Testing with Mockito Example\",\"datePublished\":\"2020-04-13T08:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/\"},\"wordCount\":582,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"articleSection\":[\"Mockito\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/\",\"name\":\"Unit Testing with Mockito Example - Examples Java Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"datePublished\":\"2020-04-13T08:00:00+00:00\",\"description\":\"In this post, we feature a comprehensive article about Java Unit Testing with Mockito Example. 1. Introduction Java unit testing is a software testing\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-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\":\"Mockito\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/mockito\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Java Unit Testing with Mockito 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":"Unit Testing with Mockito Example - Examples Java Code Geeks - 2026","description":"In this post, we feature a comprehensive article about Java Unit Testing with Mockito Example. 1. Introduction Java unit testing is a software testing","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-mockito-example\/","og_locale":"en_US","og_type":"article","og_title":"Unit Testing with Mockito Example - Examples Java Code Geeks - 2026","og_description":"In this post, we feature a comprehensive article about Java Unit Testing with Mockito Example. 1. Introduction Java unit testing is a software testing","og_url":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2020-04-13T08:00:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-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":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/"},"author":{"name":"Mary Zheng","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/8a2034fbabcb20a9396e9819261855ae"},"headline":"Java Unit Testing with Mockito Example","datePublished":"2020-04-13T08:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/"},"wordCount":582,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","articleSection":["Mockito"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/","name":"Unit Testing with Mockito Example - Examples Java Code Geeks - 2026","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","datePublished":"2020-04-13T08:00:00+00:00","description":"In this post, we feature a comprehensive article about Java Unit Testing with Mockito Example. 1. Introduction Java unit testing is a software testing","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-unit-testing-with-mockito-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":"Mockito","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/mockito\/"},{"@type":"ListItem","position":5,"name":"Java Unit Testing with Mockito 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\/87646","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=87646"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/87646\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/21338"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=87646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=87646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=87646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}