{"id":21038,"date":"2015-03-16T15:00:17","date_gmt":"2015-03-16T13:00:17","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=21038"},"modified":"2019-04-09T13:38:39","modified_gmt":"2019-04-09T10:38:39","slug":"mockito-hello-world-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/","title":{"rendered":"Mockito Hello World Example"},"content":{"rendered":"<p>You are here, it means either you are interested in the mock objects or you are already aware of <a href=\"http:\/\/mockito.org\/\">Mockito<\/a>, and you want to see a working example of it. Well&#8230;let me first introduce you to Mockito which is an open source mock unit testing framework for Java.&nbsp;<span style=\"line-height: 1.5\">In this article, I am going to show you a Hello World example of <\/span><a style=\"line-height: 1.5\" href=\"http:\/\/mockito.org\/\">Mockito<\/a><span style=\"line-height: 1.5\">. You will learn how to use it for mock object creation, stubbing and verification. I&#8217;ll also explain in detail how mock objects work, how they encourage testing based on behavior verification.<\/span><\/p>\n<p>My setup consists of :<\/p>\n<ul>\n<li><a class=\"ext-link\" title=\"\" href=\"http:\/\/maven.apache.org\/\" rel=\"external nofollow\">Maven<\/a>&nbsp;&#8211; the&nbsp;build tool<\/li>\n<li><a class=\"ext-link\" title=\"\" href=\"http:\/\/www.eclipse.org\/\" rel=\"external nofollow\">Eclipse <\/a>&#8211;&nbsp;IDE, version Luna 4.4.1.<\/li>\n<li><a href=\"http:\/\/testng.org\/doc\/index.html\">TestNG <\/a>&#8211;&nbsp;testing framework<\/li>\n<\/ul>\n<p>In case you are new to TestNG,&nbsp;<a href=\"http:\/\/examples.javacodegeeks.com\/enterprise-java\/testng\/testng-maven-project-example\/\">TestNG Maven Project Example<\/a>&nbsp;will guide you on&nbsp;how to setup a Maven based project and run the TestNG tests.<\/p>\n<p>We will&nbsp;start with a hello world example but first lets add <a href=\"https:\/\/maven-badges.herokuapp.com\/maven-central\/org.mockito\/mockito-core\">Mockito dependency<\/a> to our <code>pom.xml<\/code>.\n<\/p>\n<h2>1. Mockito Dependency<\/h2>\n<p>Add <code>mockito-core<\/code> to <code>pom.xml<\/code>.<\/p>\n<pre class=\"brush:xml\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns: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\t&lt;groupId&gt;com.javacodegeeks.testng.maven&lt;\/groupId&gt;\n\t&lt;artifactId&gt;testngMaven&lt;\/artifactId&gt;\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;dependencies&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.testng&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;testng&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;6.8.8&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;2.0.5-beta&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t&lt;\/dependencies&gt;\n&lt;\/project&gt;\n<\/pre>\n<h2>2. Mockito Hello World Example<\/h2>\n<p>I will be using <code>Foo<\/code> and <code>Bar<\/code> to demonstrate my example.<\/p>\n<p>Let&#8217;s bring <code>Foo<\/code> into picture and make it greet us.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Foo:<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockito;\n\npublic interface Foo {\n\tString HELLO_WORLD = \"Hello World\";\n\tString greet();\n}\n<\/pre>\n<p>Note that <code>Foo<\/code> is just an interface. Let&#8217;s imagine that the implementation would be provided by some other team which is either still working on it or is not yet done with the implementation. Now assuming that a portion of your component is dependent on <code>Foo<\/code>&#8216;s API, the idea of you waiting for the delivery of <code>Foo<\/code> is not so encouraging. This is where we will have to switch hats and apply a mockist approach to our problem.<\/p>\n<h3>2.1. Building Mock Object<\/h3>\n<p>Let&#8217;s write our first test that will allow us to peep into Mockito&#8217;s world.<\/p>\n<p>Our first scenario would be to call <code>foo.greet()<\/code> and make it return &#8220;Hello World&#8221;. This will introduce us to concepts like mocking and training the mock object.<\/p>\n<p>Our test consists of:<\/p>\n<ol>\n<li>Creating a mock Foo object<\/li>\n<li>and then train it to return us &#8220;Hello World&#8221; when <code>Foo.greet()<\/code> is called. This will set up the expectations that we have from the mock object.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline\"><em>MockitoHelloWorldExample:<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockito;\n\nimport static com.javacodegeeks.mockito.Foo.*;\nimport static org.mockito.Mockito.*;\nimport static org.testng.Assert.*;\n\nimport org.testng.annotations.Test;\n\npublic class MockitoHelloWorldExample {\n\t@Test\n\tpublic void fooGreets() {\n\t\tFoo foo = mock(Foo.class);\n\t\twhen(foo.greet()).thenReturn(HELLO_WORLD);\n\t\tSystem.out.println(\"Foo greets: \" + foo.greet());\n\t\tassertEquals(foo.greet(), HELLO_WORLD);\n\t}\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output:<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Foo greets: Hello World\nPASSED: fooGreets\n\n===============================================\n    Default test\n    Tests run: 1, Failures: 0, Skips: 0\n===============================================\n<\/pre>\n<h3>2.2. Mockito APIs Used<\/h3>\n<p>Let&#8217;s now review the Mockito APIs that we have called.<\/p>\n<p>We have used <code>Mockito.mock(Foo.class)<\/code> to create the mock object. Since will be calling Mockito APIs quite often, we can improve the clarity of API call by statically importing the package <code>org.mockito.Mockito.*<\/code>. We don&#8217;t have to make the explicit static calls any more. If you notice in our above test, to create the mock object, I have simply called <code>mock(Foo.class)<\/code>.<\/p>\n<p>The next thing we do is to setup our expectations. Our expectation is, when&nbsp;<code>foo.greet()<\/code> is called then return &#8216;Hello World&#8217;. The API construction is designed to be more readable and English like. To achieve it, we call:<\/p>\n<pre class=\"brush:java\">when(foo.greet()).thenReturn(HELLO_WORLD)<\/pre>\n<p>The API follows builder pattern where each method returns us an Object of type <code>OngoingStubbing<\/code> so that we can stub further on the returned object thus allowing us to build the expectations fluently.<\/p>\n<h3>2.3. System Under Test<\/h3>\n<p>Ok, this works but that&#8217;s not our goal. <code>Foo<\/code> is only a collaborator and not the system under test, also fondly called, SUT. Let&#8217;s bring our SUT <code>Bar<\/code> into the field.<\/p>\n<p><code>Bar<\/code> has a method called <code>greet(Foo)<\/code> which takes in a Foo object, makes a call to <code>foo.greet()<\/code> and returns us <code>Foo<\/code>&#8216;s greeting.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Bar:<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockito;\n\npublic class Bar {\n\tpublic String greet(Foo foo) {\n\t\tSystem.out.println(\"Bar invokes Foo.greet\");\n\t\treturn foo.greet();\n\t}\n}\n<\/pre>\n<p>We will now add our new test <code>barGreets()<\/code> which just makes sure that Foo returns us the proper response. Since even our second test makes use of the mock object, we have moved the setting up of mock object to our new configuration method <code>setupMock(),<\/code> which is a <code>@BeforeMethod<\/code>&nbsp;that gets called right before the invocation of each test method.<\/p>\n<p><span style=\"text-decoration: underline\"><em>MockitoHelloWorldExample:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[13,14,15,16,17,25,26,27,28,29]\">package com.javacodegeeks.mockito;\n\nimport static com.javacodegeeks.mockito.Foo.*;\nimport static org.mockito.Mockito.*;\nimport static org.testng.Assert.*;\n\nimport org.testng.annotations.BeforeMethod;\nimport org.testng.annotations.Test;\n\npublic class MockitoHelloWorldExample {\n\tprivate Foo foo;\n\t\n\t@BeforeMethod\n\tpublic void setupMock() {\t\t\n\t\tfoo = mock(Foo.class);\n\t\twhen(foo.greet()).thenReturn(HELLO_WORLD);\n\t}\n\t\n\t@Test\n\tpublic void fooGreets() {\n\t\tSystem.out.println(\"Foo greets: \" + foo.greet());\n\t\tassertEquals(HELLO_WORLD, foo.greet());\n\t}\n\t\n\t@Test\n\tpublic void barGreets() {\n\t\tBar bar = new Bar();\n\t\tassertEquals(HELLO_WORLD, bar.greet(foo));\n\t}\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output:<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Bar invokes Foo.greet\nPASSED: barGreets\n\n===============================================\n    Default test\n    Tests run: 1, Failures: 0, Skips: 0\n===============================================\n<\/pre>\n<p>Ok, that looks good. We have a SUT and a collaborator. We are able to test SUT even though the actual collaborator implementation is not yet available. Thanks to the Mock object!.<\/p>\n<p>If you notice, <code>Bar<\/code> is way too simplistic. Let&#8217;s spice it up and add a few conditions.<\/p>\n<h2>3. Behavior Verification<\/h2>\n<p>We will now add a new method to <code>Bar<\/code> called <code>question(Foo foo, String question)<\/code> which takes in a question, sends it to <code>Foo<\/code> and then returns us <code>Foo<\/code>&#8216;s answer. As promised, we will spice it up a bit with a few conditions:<\/p>\n<ol>\n<li>First, we need to make sure <code>Foo<\/code> is available. We will know&nbsp;it is available when&nbsp;<code>foo.greet()<\/code> returns us &#8220;Hello World&#8221;.<\/li>\n<li>If <code>Foo<\/code>&nbsp;is unavailable, <code>Bar<\/code> will not question <code>Foo<\/code> any more and instead will throw <code>FooNotAvailable<\/code> exception.<\/li>\n<li><code>Foo<\/code> will answer only certain questions..<\/li>\n<li>Any other question sent, <code>Bar<\/code> will simply filter it out, without requesting <code>Foo<\/code> for an answer and instead will return &#8220;Invalid Question&#8221;.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline\"><em>Bar:<\/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=\"brush:java; highlight:[9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]\">package com.javacodegeeks.mockito;\n\npublic class Bar {\n\tpublic String greet(Foo foo) {\n\t\tSystem.out.println(\"Bar invokes Foo.greet\");\n\t\treturn foo.greet();\n\t}\n\t\n\tpublic String question(Foo foo, String question) {\n\t\tverifyFooConnection(foo);\n\t\tif (Foo.ANY_NEW_TOPICS.equals(question)) {\n\t\t\treturn foo.question(question);\n\t\t}\n\t\treturn \"Invalid Question\";\n\t}\n\n\tpublic void verifyFooConnection(Foo foo) {\n\t\tSystem.out.println(\"Is Foo available?\");\n\t\tString response = foo.greet();\n\t\tif (!Foo.HELLO_WORLD.equals(response)) {\n\t\t\tSystem.out.println(\"No\");\n\t\t\tthrow new FooNotAvailable();\n\t\t}\n\t\tSystem.out.println(\"Yes\");\n\t}\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Foo:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[7]\">package com.javacodegeeks.mockito;\n\npublic interface Foo {\n\tString HELLO_WORLD = \"Hello World\";\n\tString ANY_NEW_TOPICS = \"Are there any new topics?\";\n\tString greet();\t\n\tString question(String question);\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>FooNotAvailable:<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockito;\n\npublic class FooNotAvailable extends RuntimeException {\n\n}\n<\/pre>\n<p>Now let&#8217;s add few tests and see how our SUT responds.<\/p>\n<p><span style=\"text-decoration: underline\"><em>MockitoHelloWorldExample:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58]\">package com.javacodegeeks.mockito;\n\nimport static com.javacodegeeks.mockito.Foo.*;\nimport static org.mockito.Mockito*;\nimport static org.testng.Assert.*;\n\nimport org.testng.annotations.BeforeMethod;\nimport org.testng.annotations.Test;\n\npublic class MockitoHelloWorldExample {\n\tprivate Foo foo;\t\n\t\n\t@BeforeMethod\n\tpublic void setupMock() {\t\t\n\t\tfoo = mock(Foo.class);\n\t\twhen(foo.greet()).thenReturn(HELLO_WORLD);\n\t}\n\t\n\t@Test\n\tpublic void fooGreets() {\n\t\tSystem.out.println(\"Foo greets: \" + foo.greet());\n\t\tassertEquals(HELLO_WORLD, foo.greet());\n\t}\n\t\n\t@Test\n\tpublic void barGreets() {\n\t\tBar bar = new Bar();\n\t\tassertEquals(HELLO_WORLD, bar.greet(foo));\n\t}\n\t\n\t@Test(expectedExceptions=FooNotAvailable.class)\n\tpublic void fooNotAvailable() {\n\t\tBar bar = new Bar();\n\t\tSystem.out.println(\"Foo is down so will not respond\");\n\t\twhen(foo.greet()).thenReturn(null);\n\t\tSystem.out.println(\"Bar sends a question to Foo but since Foo is not avilable will throw FooNotAvailable\");\n\t\tbar.question(foo, \"Hello Foo\");\t\n\t}\n\t\n\t@Test\n\tpublic void barQuestionsFoo() {\n\t\tBar bar = new Bar();\n\t\tSystem.out.println(\"Bar asks Foo 'Any new topics?', it should get a response\"); \n\t\tbar.question(foo, Foo.ANY_NEW_TOPICS);\t\n\t\tSystem.out.println(\"Verify that Foo has been asked the question\");\n\t\tverify(foo, times(1)).question(Foo.ANY_NEW_TOPICS);\t\t\n\t}\n\t\n\t@Test\n\tpublic void filterInvalidQuestions() {\n\t\tBar bar = new Bar();\t\t\n\t\tString invalidQuestion = \"Invalid question\";\n\t\tbar.question(foo, invalidQuestion);\t\n\t\tSystem.out.println(\"Verify that question was never requested as Foo is un-available\");\n\t\tverify(foo, never()).question(invalidQuestion);\n\t}\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output:<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Foo is down so will not respond\nBar sends a question to Foo but since Foo is not avilable will throw FooNotAvailable\nIs Foo available?\nNo\nPASSED: fooNotAvailable\n\nBar asks Foo 'Any new topics?', it should get a response\nIs Foo available?\nYes\nVerify that Foo has been asked the question\nPASSED: barQuestionsFoo\n\nIs Foo available?\nYes\nVerify that question was never requested as Foo is unavailable\nPASSED: filterInvalidQuestions\n<\/pre>\n<p>Let&#8217;s review each test case.<\/p>\n<ol>\n<li><code>fooNotAvailable()<\/code> tests whether <code>Bar.question<\/code> throws <code>FooNotAvailable<\/code> exception when <code>Foo<\/code> is unavailable. We train the mock object <code>Foo<\/code> to return null when <code>greet()<\/code> is called. <code>@Test<\/code> attribute <code>expectedExceptions<\/code> asserts our expected exception.<\/li>\n<li><code>barQuestionsFoo<\/code> sends a valid question to the <code>Bar<\/code> and then verifies whether it has been delegated to <code>Foo.question<\/code> for an answer.<\/li>\n<li><code>filterInvalidQuestions<\/code> sends an invalid question to the <code>Bar<\/code> and then verifies that <code>Foo.question<\/code> has not been called.<\/li>\n<\/ol>\n<h2>4. Mock Object throwing Exceptions<\/h2>\n<p>Till now, it was <code>Bar<\/code>, deciding whether a question is valid. Let&#8217;s push this responsibility to <code>Foo.<\/code>&nbsp;This makes sense as it is <code>Foo<\/code> which has to decide whether to answer or not. Since <code>Foo<\/code> now knows which one is valid and which is not, in case of an invalid question, it will reject the question and throw an <code>InvalidQuestion<\/code> exception. With this change, our <code>Foo<\/code> interface looks like below.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Foo:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[8]\">package com.javacodegeeks.mockito;\n\npublic interface Foo {\n\tString HELLO_WORLD = \"Hello World\";\n\tString ANY_NEW_TOPICS = \"Are there any new topics?\";\n\tString greet();\n\tString question(String question);\n\tString questionStrictly(String question) throws InvalidQuestion;\n}\n<\/pre>\n<p>Few points to note about <code>Foo<\/code>.<\/p>\n<ol>\n<li>We have added a new method <code>questionStrictly(question)<\/code> which strictly verifies whether a question is valid.<\/li>\n<li>In case of an invalid question, it is expected to throw <code>InvalidQuestion<\/code>.<\/li>\n<li>Else it is supposed to return an answer.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline\"><em>Bar:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[17,18,19,20,21]\">package com.javacodegeeks.mockito;\n\npublic class Bar {\n\tpublic String greet(Foo foo) {\n\t\tSystem.out.println(\"Bar invokes Foo.greet\");\n\t\treturn foo.greet();\n\t}\n\t\n\tpublic String question(Foo foo, String question) {\n\t\tverifyFooConnection(foo);\n\t\tif (Foo.ANY_NEW_TOPICS.equals(question)) {\n\t\t\treturn foo.question(question);\n\t\t}\n\t\treturn \"Invalid Question\";\n\t}\n\t\n\tpublic String questionStrictly(Foo foo, String question) throws InvalidQuestion {\n\t\tverifyFooConnection(foo);\n\t\tString answer= foo.questionStrictly(question);\t\t\t\t\n\t\treturn answer;\n\t}\n\t\n\tpublic void verifyFooConnection(Foo foo) {\n\t\tSystem.out.println(\"Is Foo available?\");\n\t\tString response = foo.greet();\n\t\tif (!Foo.HELLO_WORLD.equals(response)) {\n\t\t\tSystem.out.println(\"No\");\n\t\t\tthrow new FooNotAvailable();\n\t\t}\n\t\tSystem.out.println(\"Yes\");\n\t}\t\t\n}\n<\/pre>\n<p>Let&#8217;s now add a test case <code>throwExceptionIfInvalidQuestion<\/code> to assert whether <code>Foo<\/code> will reject an invalid question.<\/p>\n<p><span style=\"text-decoration: underline\"><em>MockitoHelloWorldExample:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[62,63,64,65,66,67,68,69,70,71,72,73,74,75]\">package com.javacodegeeks.mockito;\n\nimport static com.javacodegeeks.mockito.Foo.*;\nimport static org.mockito.Matchers.argThat;\nimport static org.mockito.Mockito.*;\nimport static org.testng.Assert.*;\n\nimport org.mockito.ArgumentMatcher;\nimport org.mockito.invocation.InvocationOnMock;\nimport org.mockito.stubbing.Answer;\nimport org.testng.annotations.BeforeMethod;\nimport org.testng.annotations.Test;\npublic class MockitoHelloWorldExample {\n\tprivate Foo foo;\t\n\tprivate final static ValidQuestions VALID_QUESTIONS = new ValidQuestions();\n\t\n\t@BeforeMethod\n\tpublic void setupMock() {\t\t\n\t\tfoo = mock(Foo.class);\n\t\twhen(foo.greet()).thenReturn(HELLO_WORLD);\n\t}\n\t\n\t@Test\n\tpublic void fooGreets() {\n\t\tSystem.out.println(\"Foo greets: \" + foo.greet());\n\t\tassertEquals(HELLO_WORLD, foo.greet());\n\t}\n\t\n\t@Test\n\tpublic void barGreets() {\n\t\tBar bar = new Bar();\n\t\tassertEquals(HELLO_WORLD, bar.greet(foo));\n\t}\n\t\n\t@Test(expectedExceptions=FooNotAvailable.class)\n\tpublic void fooNotAvailable() {\n\t\tBar bar = new Bar();\n\t\tSystem.out.println(\"Foo is down so will not respond\");\n\t\twhen(foo.greet()).thenReturn(null);\n\t\tSystem.out.println(\"Bar sends a question to Foo but since Foo is not avilable will throw FooNotAvailable\");\n\t\tbar.question(foo, \"Hello Foo\");\t\n\t}\n\t\n\t@Test\n\tpublic void barQuestionsFoo() {\n\t\tBar bar = new Bar();\n\t\tSystem.out.println(\"Bar asks Foo 'Any new topics?', it should get a response\"); \n\t\tbar.question(foo, Foo.ANY_NEW_TOPICS);\t\n\t\tSystem.out.println(\"Verify that Foo has been asked the question\");\n\t\tverify(foo, times(1)).question(Foo.ANY_NEW_TOPICS);\t\t\n\t}\n\t\n\t@Test\n\tpublic void filterInvalidQuestions() {\n\t\tBar bar = new Bar();\t\t\n\t\tString invalidQuestion = \"Invalid question\";\n\t\tbar.question(foo, invalidQuestion);\t\n\t\tSystem.out.println(\"Verify that question was never requested as Foo is un-available\");\n\t\tverify(foo, never()).question(invalidQuestion);\n\t}\n\t\n\t@Test(expectedExceptions=InvalidQuestion.class)\n\tpublic void throwExceptionIfInvalidQuestion() throws InvalidQuestion {\n\t\tBar bar = new Bar();\t\t\n\t\tString invalidQuestion = \"Invalid question\";\n\t\twhen(foo.questionStrictly(\"Invalid question\")).thenThrow(new InvalidQuestion());\n\t\tbar.questionStrictly(foo, invalidQuestion);\n\t}\t\n}\n<\/pre>\n<p>Our new scenario is, when <code>foo.questionStrictly()<\/code> is passed an invalid question, <code>Foo<\/code> should throw <code>InvalidQuestion<\/code>. The exception expected is setup using <code>thenThrow()<\/code> API which accepts the exception to be thrown. After the setup, <code>bar.questionStrictly()<\/code> is called with the invalid question. Our <code>expectedExceptions<\/code> test attribute makes sure that the exception is thrown.[ulp id=&#8217;GhnY46zldrhgoeym&#8217;]<\/p>\n<pre class=\"brush:java\">when(foo.questionStrictly(\"Invalid question\")).thenThrow(new InvalidQuestion());\n<\/pre>\n<h2>5. More Behavior Verification using ArgumentMatcher and Answer Callback<\/h2>\n<p>We will further modify our <code>Bar<\/code> class so that now it can respond to the answers received from <code>Foo<\/code>. Based on the answer received, it will make further calls to <code>Foo<\/code>.<br \/>\n<code>Bar<\/code> will ask <code>Foo<\/code>, whether there are any new topics of discussion. For example, a tutorial topic. <code>Foo<\/code> may either answer in affirmative or negative. If they are no new topics, <code>Bar<\/code> will call <code>foo.bye()<\/code> to indicate the end of discussion.<br \/>\nIf there are new topics, <code>Bar<\/code> will further ask <code>Foo<\/code>&nbsp;the current day&#8217;s topic and its price. Once it receives the price, it will call <code>foo.bye()<\/code> to end the session.<\/p>\n<p>Let&#8217;s see our modified <code>Bar<\/code> class.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Bar:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]\">package com.javacodegeeks.mockito;\n\npublic class Bar {\n\tpublic String greet(Foo foo) {\n\t\tSystem.out.println(\"Bar invokes Foo.greet\");\n\t\treturn foo.greet();\n\t}\n\t\n\tpublic String question(Foo foo, String question) {\n\t\tverifyFooConnection(foo);\n\t\tif (Foo.ANY_NEW_TOPICS.equals(question)) {\n\t\t\treturn foo.question(question);\n\t\t}\n\t\treturn \"Invalid Question\";\n\t}\n\t\n\tpublic String questionStrictly(Foo foo, String question) throws InvalidQuestion {\n\t\tverifyFooConnection(foo);\n\t\tSystem.out.println(question);\n\t\tString answer= foo.questionStrictly(question);\t\t\n\t\tswitch (answer) {\n\t\tcase Foo.NO_NEW_TOPIC:\n\t\t\tSystem.out.println(\"No\");\n\t\t\tSystem.out.println(\"Let's quit now\");\n\t\t\tfoo.bye();\n\t\t\tbreak;\n\t\tcase Foo.YES_NEW_TOPICS_AVAILABLE:\n\t\t\tSystem.out.println(\"Yes\");\n\t\t\tSystem.out.println(Foo.WHAT_IS_TODAYS_TOPIC);\n\t\t\tanswer = foo.questionStrictly(Foo.WHAT_IS_TODAYS_TOPIC);\n\t\t\tSystem.out.println(\"Topic name is \" + answer);\n\t\t\tSystem.out.println(\"What is the price?\");\n\t\t\tint price = foo.getPrice(answer);\n\t\t\tSystem.out.println(\"Price is \" + price); \n\t\t\tSystem.out.println(\"Let's quit now\");\n\t\t\tfoo.bye();\n\t\t\tanswer = \"Topic is \" + answer + \", price is \" + price;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tSystem.out.println(\"Answer is \" + answer);\n\t\t\tbreak;\n\t\t}\n\t\treturn answer;\n\t}\n\t\n\tpublic void verifyFooConnection(Foo foo) {\n\t\tSystem.out.println(\"Is Foo available?\");\n\t\tString response = foo.greet();\n\t\tif (!Foo.HELLO_WORLD.equals(response)) {\n\t\t\tSystem.out.println(\"No\");\n\t\t\tthrow new FooNotAvailable();\n\t\t}\n\t\tSystem.out.println(\"Yes\");\n\t}\t\n}\n<\/pre>\n<p>New additions to <code>Foo<\/code> are the APIs <code>getPrice(tutorial)<\/code> and <code>bye()<\/code> and a few constants.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Foo:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[6,7,8,9,11,14]\">package com.javacodegeeks.mockito;\n\npublic interface Foo {\n\tString HELLO_WORLD = \"Hello World\";\n\tString ANY_NEW_TOPICS = \"Are there any new topics?\";\n\tString WHAT_IS_TODAYS_TOPIC = \"What is todays topic?\";\n\tString YES_NEW_TOPICS_AVAILABLE = \"Yes\";\n\tString NO_NEW_TOPIC = \"No\";\n\tString TOPIC_MOCKITO = \"Mockito\";\n\tString greet();\n\tint getPrice(String tutorial);\n\tString question(String question);\n\tString questionStrictly(String question) throws InvalidQuestion;\n\tvoid bye();\n}\n<\/pre>\n<p>In our previous test case <code>throwExceptionIfInvalidQuestion<\/code>, we had explicitly checked for &#8220;Invalid Question&#8221; but note that there can be more questions which fall into the invalid zone.&nbsp;Also, since <code>Bar<\/code> now responds to answers, we need to setup our mock object to map the questions and answers.<\/p>\n<p><span style=\"text-decoration: underline\"><em>MockitoHelloWorldExample:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140]\">package com.javacodegeeks.mockito;\n\nimport static com.javacodegeeks.mockito.Foo.*;\nimport static org.mockito.Matchers.argThat;\nimport static org.mockito.Mockito.*;\nimport static org.testng.Assert.*;\n\nimport org.mockito.ArgumentMatcher;\nimport org.mockito.invocation.InvocationOnMock;\nimport org.mockito.stubbing.Answer;\nimport org.testng.annotations.BeforeMethod;\nimport org.testng.annotations.Test;\n\npublic class MockitoHelloWorldExample {\n\tprivate Foo foo;\n\tprivate final static ValidQuestions VALID_QUESTIONS = new ValidQuestions();\n\n\t@BeforeMethod\n\tpublic void setupMock() {\n\t\tfoo = mock(Foo.class);\n\t\twhen(foo.greet()).thenReturn(HELLO_WORLD);\n\t}\n\n\t@Test\n\tpublic void fooGreets() {\n\t\tSystem.out.println(\"Foo greets: \" + foo.greet());\n\t\tassertEquals(HELLO_WORLD, foo.greet());\n\t}\n\n\t@Test\n\tpublic void barGreets() {\n\t\tBar bar = new Bar();\n\t\tassertEquals(HELLO_WORLD, bar.greet(foo));\n\t}\n\n\t@Test(expectedExceptions = FooNotAvailable.class)\n\tpublic void fooNotAvailable() {\n\t\tBar bar = new Bar();\n\t\tSystem.out.println(\"Foo is down so will not respond\");\n\t\twhen(foo.greet()).thenReturn(null);\n\t\tSystem.out\n\t\t\t\t.println(\"Bar sends a question to Foo but since Foo is not avilable will throw FooNotAvailable\");\n\t\tbar.question(foo, \"Hello Foo\");\n\t}\n\n\t@Test\n\tpublic void barQuestionsFoo() {\n\t\tBar bar = new Bar();\n\t\tSystem.out\n\t\t\t\t.println(\"Bar asks Foo 'Any new topics?', it should get a response\");\n\t\tbar.question(foo, Foo.ANY_NEW_TOPICS);\n\t\tSystem.out.println(\"Verify that Foo has been asked the question\");\n\t\tverify(foo, times(1)).question(Foo.ANY_NEW_TOPICS);\n\t}\n\n\t@Test\n\tpublic void filterInvalidQuestions() {\n\t\tBar bar = new Bar();\n\t\tString invalidQuestion = \"Invalid question\";\n\t\tbar.question(foo, invalidQuestion);\n\t\tSystem.out\n\t\t\t\t.println(\"Verify that question was never requested as Foo is un-available\");\n\t\tverify(foo, never()).question(invalidQuestion);\n\t}\n\n\t@Test(expectedExceptions = InvalidQuestion.class)\n\tpublic void throwExceptionIfInvalidQuestion() throws InvalidQuestion {\n\t\tBar bar = new Bar();\n\t\tString invalidQuestion = \"Invalid question\";\n\t\twhen(foo.questionStrictly(\"Invalid question\")).thenThrow(\n\t\t\t\tnew InvalidQuestion());\n\t\tbar.questionStrictly(foo, invalidQuestion);\n\t}\n\n\t@Test(expectedExceptions = InvalidQuestion.class)\n\tpublic void throwExceptionIfAnyInvalidQuestion() throws InvalidQuestion {\n\t\tBar bar = new Bar();\n\t\tString invalidQuestion = \"Invalid question\";\n\t\twhen(foo.questionStrictly(argThat(new InValidQuestions()))).thenThrow(\n\t\t\t\tnew InvalidQuestion());\n\t\tbar.questionStrictly(foo, invalidQuestion);\n\t}\n\n\t@Test\n\tpublic void getTodaysTopicPrice() throws InvalidQuestion {\n\t\tBar bar = new Bar();\n\t\twhen(foo.questionStrictly(argThat(new ValidQuestions()))).thenAnswer(\n\t\t\t\tnew FooAnswers());\n\t\twhen(foo.getPrice(TOPIC_MOCKITO)).thenReturn(20);\n\n\t\tString answer = bar.questionStrictly(foo, ANY_NEW_TOPICS);\n\t\tSystem.out.println(\"Answer is: \" + answer);\n\t\tassertEquals(answer, \"Topic is Mockito, price is 20\");\n\t\tverify(foo, times(1)).questionStrictly(WHAT_IS_TODAYS_TOPIC);\n\t\tverify(foo, times(1)).getPrice(TOPIC_MOCKITO);\n\t\tverify(foo, times(1)).bye();\n\t}\n\n\t@Test\n\tpublic void noNewTopic() throws InvalidQuestion {\n\t\tBar bar = new Bar();\n\t\twhen(foo.questionStrictly(ANY_NEW_TOPICS)).thenReturn(NO_NEW_TOPIC);\n\n\t\tString answer = bar.questionStrictly(foo, ANY_NEW_TOPICS);\n\t\tSystem.out.println(\"Answer is: \" + answer);\n\t\tassertEquals(answer, NO_NEW_TOPIC);\n\t\tverify(foo, times(1)).bye();\n\t\tverify(foo, never()).questionStrictly(WHAT_IS_TODAYS_TOPIC);\n\t\tverify(foo, never()).getPrice(TOPIC_MOCKITO);\n\t}\n\n\tprivate static class InValidQuestions extends ArgumentMatcher {\n\n\t\t@Override\n\t\tpublic boolean matches(Object argument) {\n\t\t\treturn !VALID_QUESTIONS.matches(argument);\n\t\t}\n\t}\n\n\tprivate static class ValidQuestions extends ArgumentMatcher {\n\n\t\t@Override\n\t\tpublic boolean matches(Object argument) {\n\t\t\treturn argument.equals(ANY_NEW_TOPICS)\n\t\t\t\t\t|| argument.equals(WHAT_IS_TODAYS_TOPIC);\n\t\t}\n\t}\n\n\tprivate static class FooAnswers implements Answer {\n\t\tpublic String answer(InvocationOnMock invocation) throws Throwable {\n\t\t\tString arg = (String) invocation.getArguments()[0];\n\t\t\tif (ANY_NEW_TOPICS.equals(arg)) {\n\t\t\t\treturn YES_NEW_TOPICS_AVAILABLE;\n\t\t\t} else if (WHAT_IS_TODAYS_TOPIC.equals(arg)) {\n\t\t\t\treturn TOPIC_MOCKITO;\n\t\t\t} else {\n\t\t\t\tthrow new InvalidQuestion();\n\t\t\t}\n\t\t}\n\t}\n\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output:<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Is Foo available?\nYes\nInvalid question\nPASSED: throwExceptionIfAnyInvalidQuestion\n\nIs Foo available?\nYes\nAre there any new topics?\nYes\nWhat is todays topic?\nTopic name is Mockito\nWhat is the price?\nPrice is 20\nLet's quit now\nAnswer is: Topic is Mockito, price is 20\nPASSED: getTodaysTopicPrice\n\nIs Foo available?\nYes\nAre there any new topics?\nNo\nLet's quit now\nAnswer is: No\nPASSED: noNewTopic\n<\/pre>\n<p>Mock objects return expected values. But when it needs to return different values for different arguments, Mockito&#8217;s argument matcher comes into play. In our case, the system has to behave in one way if the questions asked are valid ones and in a different way if they are &#8216;invalid&#8217; which the collaborator doesn&#8217;t know how to respond.<\/p>\n<p>Let&#8217;s go through our new test cases:<\/p>\n<ol>\n<li><code>throwExceptionIfAnyInvalidQuestion<\/code> &#8211; instead of testing the code against one invalid value, it now tests on a sub-set of values using the <code>ArgumentMatcher<\/code>\n<pre class=\"brush:java\">when(foo.questionStrictly(argThat(new InValidQuestions()))).thenThrow(new InvalidQuestion());\n<\/pre>\n<p>We pass in a <code><code>org.mockito.ArgumentMatcher<\/code><\/code> object to <code>argThat(),<\/code> so that the argument passed in to <code>foo.questionStrictly()<\/code> can be tested against the matcher to know whether it is one of the arguments expected by the mock object. If yes, then the next stub action will follow, in our case, the method will throw an <code>InvalidQuestion<\/code> exception, if the argument value is not a valid question.<\/li>\n<li><code>getTodaysTopicPrice<\/code> &#8211; here our <code>Bar<\/code> asks <code>Foo<\/code> whether there are new tutorials. The question asked is one of the valid ones so&nbsp;<code>Foo<\/code> responds with the current topic. <code>Bar<\/code> then asks for the price of the latest tutorial. Finally,&nbsp;<code>Bar<\/code> requests <code>Foo<\/code> to end the session calling <code>foo.bye()<\/code>. We setup our expectations on the mock object using the below statements.\n<pre class=\"brush:java\">\t\twhen(foo.questionStrictly(argThat(new ValidQuestions()))).thenAnswer(\n\t\t\t\tnew FooAnswers());\n\t\twhen(foo.getPrice(TOPIC_MOCKITO)).thenReturn(20);\n<\/pre>\n<p>After setting up, we call on the actual SUT method to start the test.<\/p>\n<pre class=\"brush:java\">bar.questionStrictly(foo, ANY_NEW_TOPICS)\n<\/pre>\n<p>After this, we do the verification to make sure that <code>questionStrictly<\/code> has interacted with <code>Foo<\/code> the way we wanted.<br \/>\nOnce <code>Foo<\/code> responds that there are new topics, <code>Bar<\/code> asks <code>Foo<\/code> further details about the topic and then finally exits.<br \/>\nWe do the verification of the calls made to Foo below:<\/p>\n<pre class=\"brush:java\">\t\tverify(foo, times(1)).questionStrictly(WHAT_IS_TODAYS_TOPIC);\n\t\tverify(foo, times(1)).getPrice(TOPIC_MOCKITO);\n\t\tverify(foo, times(1)).bye();\n<\/pre>\n<\/li>\n<li><code>noNewTopic<\/code> &#8211; here <code>Foo<\/code> returns response with no new topics in which case, <code>Bar<\/code> calls on <code>foo.bye()<\/code> to exit the communication<\/li>\n<\/ol>\n<h2>Download the Eclipse project<\/h2>\n<p>This was a Mockito Hello World Example.<\/p>\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\/2015\/03\/mockitoHelloWorld.zip\"><strong>mockitoHelloWorld<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are here, it means either you are interested in the mock objects or you are already aware of Mockito, and you want to see a working example of it. Well&#8230;let me first introduce you to Mockito which is an open source mock unit testing framework for Java.&nbsp;In this article, I am going to show &hellip;<\/p>\n","protected":false},"author":38,"featured_media":21338,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[923],"tags":[],"class_list":["post-21038","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>Mockito Hello World Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"You are here, it means either you are interested in the mock objects or you are already aware of Mockito, and you want to see a working example of it.\" \/>\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-development\/core-java\/mockito\/mockito-hello-world-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mockito Hello World Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"You are here, it means either you are interested in the mock objects or you are already aware of Mockito, and you want to see a working example of it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-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=\"2015-03-16T13:00:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-09T10:38:39+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=\"Ram Mokkapaty\" \/>\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=\"Ram Mokkapaty\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/\"},\"author\":{\"name\":\"Ram Mokkapaty\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/7b1823eb5bd673bd375f8bee33b70cd8\"},\"headline\":\"Mockito Hello World Example\",\"datePublished\":\"2015-03-16T13:00:17+00:00\",\"dateModified\":\"2019-04-09T10:38:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/\"},\"wordCount\":1480,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-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-development\/core-java\/mockito\/mockito-hello-world-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/\",\"name\":\"Mockito Hello World Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"datePublished\":\"2015-03-16T13:00:17+00:00\",\"dateModified\":\"2019-04-09T10:38:39+00:00\",\"description\":\"You are here, it means either you are interested in the mock objects or you are already aware of Mockito, and you want to see a working example of it.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-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-development\/core-java\/mockito\/mockito-hello-world-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\":\"Mockito Hello World 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\/7b1823eb5bd673bd375f8bee33b70cd8\",\"name\":\"Ram Mokkapaty\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Ram-Mokkapaty-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Ram-Mokkapaty-96x96.jpg\",\"caption\":\"Ram Mokkapaty\"},\"description\":\"Ram holds a master's degree in Machine Design from IT B.H.U. His expertise lies in test driven development and re-factoring. He is passionate about open source technologies and actively blogs on various java and open-source technologies like spring. He works as a principal Engineer in the logistics domain.\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\/\",\"https:\/\/in.linkedin.com\/pub\/ram-satish-mokkapaty\/18\/123\/52b\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/ram-mokkapaty\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mockito Hello World Example - Java Code Geeks","description":"You are here, it means either you are interested in the mock objects or you are already aware of Mockito, and you want to see a working example of it.","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-development\/core-java\/mockito\/mockito-hello-world-example\/","og_locale":"en_US","og_type":"article","og_title":"Mockito Hello World Example - Java Code Geeks","og_description":"You are here, it means either you are interested in the mock objects or you are already aware of Mockito, and you want to see a working example of it.","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-03-16T13:00:17+00:00","article_modified_time":"2019-04-09T10:38:39+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":"Ram Mokkapaty","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Ram Mokkapaty","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/"},"author":{"name":"Ram Mokkapaty","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/7b1823eb5bd673bd375f8bee33b70cd8"},"headline":"Mockito Hello World Example","datePublished":"2015-03-16T13:00:17+00:00","dateModified":"2019-04-09T10:38:39+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/"},"wordCount":1480,"commentCount":3,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-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-development\/core-java\/mockito\/mockito-hello-world-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/","name":"Mockito Hello World Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","datePublished":"2015-03-16T13:00:17+00:00","dateModified":"2019-04-09T10:38:39+00:00","description":"You are here, it means either you are interested in the mock objects or you are already aware of Mockito, and you want to see a working example of it.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-hello-world-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-development\/core-java\/mockito\/mockito-hello-world-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":"Mockito Hello World 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\/7b1823eb5bd673bd375f8bee33b70cd8","name":"Ram Mokkapaty","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Ram-Mokkapaty-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Ram-Mokkapaty-96x96.jpg","caption":"Ram Mokkapaty"},"description":"Ram holds a master's degree in Machine Design from IT B.H.U. His expertise lies in test driven development and re-factoring. He is passionate about open source technologies and actively blogs on various java and open-source technologies like spring. He works as a principal Engineer in the logistics domain.","sameAs":["http:\/\/www.javacodegeeks.com\/","https:\/\/in.linkedin.com\/pub\/ram-satish-mokkapaty\/18\/123\/52b"],"url":"https:\/\/examples.javacodegeeks.com\/author\/ram-mokkapaty\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/21038","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\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=21038"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/21038\/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=21038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=21038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=21038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}