{"id":21481,"date":"2015-03-23T11:00:58","date_gmt":"2015-03-23T09:00:58","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=21481"},"modified":"2019-04-09T13:37:35","modified_gmt":"2019-04-09T10:37:35","slug":"mockito-void-method-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/","title":{"rendered":"Mockito void Method Example"},"content":{"rendered":"<p>In <a href=\"http:\/\/examples.javacodegeeks.com\/core-java\/mockito\/mockito-hello-world-example\/\">Mockito Hello World Example<\/a>, we have learnt how to stub a non-void method that returns something. Sometimes we may also need to stub a void method which is what I am going to show in this article.<\/p>\n<p>Before I start with my example, a bit about my setup:<\/p>\n<ul>\n<li>I am using&nbsp;<a class=\"ext-link\" title=\"\" href=\"http:\/\/maven.apache.org\/\" rel=\"external nofollow\">Maven<\/a>&nbsp;\u2013 the&nbsp;build tool<\/li>\n<li><a class=\"ext-link\" title=\"\" href=\"http:\/\/www.eclipse.org\/\" rel=\"external nofollow\">Eclipse <\/a>&nbsp;as the&nbsp;IDE, version Luna 4.4.1.<\/li>\n<li><a class=\"ext-link\" title=\"\" href=\"http:\/\/testng.org\/doc\/index.html\" rel=\"external nofollow\">TestNG <\/a>&nbsp;is my&nbsp;testing framework, in case you are new to TestNG, please refer&nbsp;<a href=\"http:\/\/examples.javacodegeeks.com\/enterprise-java\/testng\/testng-maven-project-example\/\">TestNG Maven Project Example.<\/a><\/li>\n<li>Add <a class=\"ext-link\" title=\"\" href=\"https:\/\/maven-badges.herokuapp.com\/maven-central\/org.mockito\/mockito-core\" rel=\"external nofollow\">Mockito dependency<\/a> to our <code>pom.xml<\/code>.<\/li>\n<\/ul>\n<p>&nbsp;\n<\/p>\n<h2>1. Mockito Void Method Example<\/h2>\n<p>The example I have chosen is about a dish that a customer is going to taste. <code>Dish<\/code> object represents the dish. It has a void <code>eat()<\/code> method which the customer object will call when served with the dish. If the dish is not the one customer is expecting then it will throw <code>WrongDishException<\/code>.<\/p>\n<p>In the next few sections, I will show you different ways of stubbing the void method <code>eat()<\/code> to change its behavior.<\/p>\n<p>The usual way to stub a non-void method is:<\/p>\n<pre class=\"brush:java\">when(dish.eat()).thenReturn(\"some value\");\n<\/pre>\n<p>But note that <code>eat()<\/code> doesn&#8217;t return anything so naturally we won&#8217;t be able to use the above style of API.<\/p>\n<p>We can stub a void method to throw an exception using <code>doThrow()<\/code>. Other than that we can also make use of <code>doNothing()<\/code> and <code>doAnswer()<\/code> APIs.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Customer:<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockito;\n\npublic class Customer {\n\tpublic void eat(Dish dish) throws WrongDishException {\n\t\ttry {\n\t\t\tSystem.out.println(\"Taste the food\");\n\t\t\tdish.eat();\n\t\t\tSystem.out.println(\"Ate the food\");\n\t\t} catch (WrongDishException e) {\n\t\t\tSystem.out.println(\"Wrong dish!\");\n\t\t\tthrow e;\n\t\t} catch (NotSuchATastyException e) {\n\t\t\tSystem.out.println(\"Not very tasty\");\n\t\t\tthrow e;\n\t\t}\t\t\n\t}\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Dish:<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockito;\n\npublic interface Dish {\n\tvoid eat() throws WrongDishException;\n}\n<\/pre>\n<h2>2. Stub void method Using deprecated API stubVoid<\/h2>\n<p>Originally, <code>stubVoid()<\/code> was used for stubbing void methods with exceptions. For example, in test <code>testEatUsingStubVoid()<\/code>, we stub <code>eat()<\/code> to simply return without throwing an exception, we can do it using <code>stubVoid()<\/code> and <code>toReturn()<\/code>.<\/p>\n<pre class=\"brush:java\">stubVoid(dish).toReturn().on().eat();\n<\/pre>\n<p>Now when we call <code>customer.eat(dish)<\/code>, it doesn&#8217;t throw any exception.<\/p>\n<p>But note that <code>stubVoid()<\/code> is deprecated so we won&#8217;t use it any more. <code>doThrow()<\/code> and <code>doReturn()<\/code> replaces <code>stubVoid()<\/code> because of improved readability and consistency with the family of <code>doAnswer()<\/code> methods.<\/p>\n<p><span style=\"text-decoration: underline\"><em>MockitoVoidExample:<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockito;\n\nimport static org.mockito.Mockito.*;\nimport static org.testng.Assert.*;\n\nimport org.mockito.InOrder;\nimport org.testng.annotations.BeforeMethod;\nimport org.testng.annotations.Test;\n\npublic class MockitoVoidExample {\n\tprivate Customer customer;\n\tprivate Dish dish;\n\t\n\t@BeforeMethod\n\tpublic void setupMock() {\n\t\tcustomer = new Customer();\n\t\tdish = mock(Dish.class);\n\t}\n\t\n\t@Test\n\tpublic void testEatUsingStubVoid() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw WrongDishException using stubVoid\");\n\t\tstubVoid(dish).toReturn().on().eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t}\t\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output:<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Train dish to not throw WrongDishException using stubVoid\nTaste the food\nAte the food\nFinished the dish, no exception thrown\nPASSED: testEatUsingStubVoid\n<\/pre>\n<h2>3. Stub void method Using toReturn<\/h2>\n<p>In test <code>testEatUsingDoNothing<\/code>, we replace <code>stubVoid()<\/code> with <code>doNothing()<\/code> and <code>when()<\/code>.<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\">doNothing().when(dish).eat();\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>MockitoVoidExample:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[26,27,28,29,30,31,32]\">package com.javacodegeeks.mockito;\n\nimport static org.mockito.Mockito.*;\n\nimport org.testng.annotations.BeforeMethod;\nimport org.testng.annotations.Test;\n\npublic class MockitoVoidExample {\n\tprivate Customer customer;\n\tprivate Dish dish;\n\t\n\t@BeforeMethod\n\tpublic void setupMock() {\n\t\tcustomer = new Customer();\n\t\tdish = mock(Dish.class);\n\t}\n\t\n\t@Test\n\tpublic void testEatUsingStubVoid() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw WrongDishException using stubVoid\");\n\t\tstubVoid(dish).toReturn().on().eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t}\n\t\n\t@Test\n\tpublic void testEatUsingDoNothing() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw WrongDishException using doNothing\");\n\t\tdoNothing().when(dish).eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t}\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output:<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Train dish to not throw WrongDishException using doNothing\nTaste the food\nAte the food\nFinished the dish, no exception thrown\nPASSED: testEatUsingDoNothing\n<\/pre>\n<h2>4. Stub void method Using doThrow<\/h2>\n<p>In <code>evaluateFood()<\/code>, we stub method <code>dish.eat()<\/code> to throw <code>NotSoTastyException<\/code> using <code>doThrow()<\/code> and <code>when()<\/code> combination.<\/p>\n<pre class=\"brush:java\">doThrow(NotSuchATastyException.class).when(dish).eat();\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>MockitoVoidExample:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[37,38,39,40,41,42]\">package com.javacodegeeks.mockito;\n\nimport static org.mockito.Mockito.*;\n\nimport org.mockito.invocation.InvocationOnMock;\nimport org.mockito.stubbing.Answer;\nimport org.testng.annotations.BeforeMethod;\nimport org.testng.annotations.Test;\n\npublic class MockitoVoidExample {\n\tprivate Customer customer;\n\tprivate Dish dish;\n\t\n\t@BeforeMethod\n\tpublic void setupMock() {\n\t\tcustomer = new Customer();\n\t\tdish = mock(Dish.class);\n\t\twhen(dish.getSpice()).thenReturn(null);\n\t}\n\t\n\t@Test\n\tpublic void testEatUsingStubVoid() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw WrongDishException using stubVoid\");\n\t\tstubVoid(dish).toReturn().on().eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t}\n\t\n\t@Test\n\tpublic void testEatUsingDoNothing() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw WrongDishException using doNothing\");\n\t\tdoNothing().when(dish).eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t}\n\t\n\t@Test(expectedExceptions=NotSoTastyException.class)\n\tpublic void evaluateFood() throws WrongDishException {\n\t\tdoThrow(NotSoTastyException.class).when(dish).eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Won't reach here\");\n\t}\t\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output:<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Taste the food\nNot very tasty\nPASSED: evaluateFood\n<\/pre>\n<h2>5. Stub void method Using doAnswer<\/h2>\n<p>Suppose we want to custom behavior a method&#8217;s behavior based on the arguments passed then we can use <code>doAnswer()<\/code> API.<\/p>\n<p><code>Answer<\/code> interface specifies an action that is executed when you interact with the mock&#8217;s method. We can customize the behavior based on the mock&#8217;s method name or the method arguments which is passed to it.&nbsp;In case of non-void methods, you can even make the <code>answer<\/code> to customize the method&#8217;s return value.<\/p>\n<p>In test <code>ifSpiceThrowException()<\/code>, the customer orders for a spicy dish. If the dish is of medium spice then <code>customer.eat(dish)<\/code> will return quietly. If the dish is too spicy then the overloaded <code>eat(spice)<\/code> method is going to throw a <code>RuntimeException<\/code>.<\/p>\n<p><code>SpiceAnswer<\/code> implements <code>Answer<\/code> and based on the degree of spice, it will either throw a <code>RuntimeException<\/code> or return a value.<\/p>\n<p>We stub the custom behavior using <code>doAnswer()<\/code> and <code>when()<\/code> APIs.<\/p>\n<pre class=\"brush:java\">doAnswer(new SpiceAnswer()).when(dish).eat(spicy);\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Dish:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[5,6]\">package com.javacodegeeks.mockito;\n\npublic interface Dish {\n\tvoid eat() throws WrongDishException;\n\tvoid eat(String spicy) throws WrongDishException;\n\tString getSpice();\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>MockitoVoidExample:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67]\">package com.javacodegeeks.mockito;\n\nimport static org.mockito.Mockito.*;\n\nimport org.mockito.invocation.InvocationOnMock;\nimport org.mockito.stubbing.Answer;\nimport org.testng.annotations.BeforeMethod;\nimport org.testng.annotations.Test;\n\npublic class MockitoVoidExample {\n\tprivate Customer customer;\n\tprivate Dish dish;\n\t\n\t@BeforeMethod\n\tpublic void setupMock() {\n\t\tcustomer = new Customer();\n\t\tdish = mock(Dish.class);\n\t\twhen(dish.getSpice()).thenReturn(null);\n\t}\n\t\n\t@Test\n\tpublic void testEatUsingStubVoid() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw WrongDishException using stubVoid\");\n\t\tstubVoid(dish).toReturn().on().eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t}\n\t\n\t@Test\n\tpublic void testEatUsingDoNothing() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw WrongDishException using doNothing\");\n\t\tdoNothing().when(dish).eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t}\n\t\n\t@Test(expectedExceptions=NotSoTastyException.class)\n\tpublic void evaluateFood() throws WrongDishException {\n\t\tdoThrow(NotSoTastyException.class).when(dish).eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Won't reach here\");\n\t}\t\n\t\n\t@Test(expectedExceptions=RuntimeException.class)\n\tpublic void ifSpiceThrowException() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw NotSoTastyException when called first time and return in subsequent calls\");\n\t\tString spicy = \"spicy\";\n\t\twhen(dish.getSpice()).thenReturn(spicy);\t\t\n\t\tdoAnswer(new SpiceAnswer()).when(dish).eat(spicy);\n\t\tcustomer.eat(dish);\t\t\n\t\t\n\t\tspicy = \"too spicy\";\n\t\twhen(dish.getSpice()).thenReturn(spicy);\t\t\n\t\tdoAnswer(new SpiceAnswer()).when(dish).eat(spicy);\n\t\tcustomer.eat(dish);\t\t\n\t}\n\t\n\tprivate class SpiceAnswer implements Answer {\n\t\t@Override\n\t\tpublic String answer(InvocationOnMock invocation) throws Throwable {\n\t\t\tString arg = (String) invocation.getArguments()[0];\n\t\t\tif (\"too spicy\".equals(arg)) {\n\t\t\t\tthrow new RuntimeException(\"Spicy dish!\");\n\t\t\t}\n\t\t\treturn arg;\n\t\t}\t\t\n\t}\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output:<\/em><\/span>[ulp id=&#8217;GhnY46zldrhgoeym&#8217;]<\/p>\n<pre class=\"brush:bash\">Train dish to not throw NotSoTastyException when called first time and return in subsequent calls\nTaste the food\nAte the food\nTaste the food\nPASSED: ifSpiceThrowException\n<\/pre>\n<h2>6. Stub void method with consecutive calls<\/h2>\n<p>Sometimes we may need to stub a method with different behaviors for&nbsp;each&nbsp;consecutive call of the same method.<br \/>\nIn test <code>eatMultipleDishes()<\/code>, <code>NotSoTastyException<\/code> is thrown the first time <code>customer.eat(dish)<\/code> is called. But no exception is thrown in the subsequent calls to <code>customer.eat(dish)<\/code>.<\/p>\n<pre class=\"brush:java\">doThrow(NotSoTastyException.class).doNothing().when(dish).eat();\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>MockitoVoidExample:<\/em><\/span><\/p>\n<pre class=\"brush:java; highlight:[70,71,72,73,74,75,76,77,78,79,80,81,82,83,84]\">package com.javacodegeeks.mockito;\n\nimport static org.mockito.Mockito.*;\n\nimport org.mockito.invocation.InvocationOnMock;\nimport org.mockito.stubbing.Answer;\nimport org.testng.Assert;\nimport org.testng.annotations.BeforeMethod;\nimport org.testng.annotations.Test;\n\npublic class MockitoVoidExample {\n\tprivate Customer customer;\n\tprivate Dish dish;\n\t\n\t@BeforeMethod\n\tpublic void setupMock() {\n\t\tcustomer = new Customer();\n\t\tdish = mock(Dish.class);\n\t\twhen(dish.getSpice()).thenReturn(null);\n\t}\n\t\n\t@Test\n\tpublic void testEatUsingStubVoid() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw WrongDishException using stubVoid\");\n\t\tstubVoid(dish).toReturn().on().eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t}\n\t\n\t@Test\n\tpublic void testEatUsingDoNothing() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw WrongDishException using doNothing\");\n\t\tdoNothing().when(dish).eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t}\n\t\n\t@Test(expectedExceptions=NotSoTastyException.class)\n\tpublic void evaluateFood() throws WrongDishException {\n\t\tdoThrow(NotSoTastyException.class).when(dish).eat();\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Won't reach here\");\n\t}\t\n\t\n\t@Test(expectedExceptions=RuntimeException.class)\n\tpublic void ifSpiceThrowException() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw NotSuchATastyException when called first time and retun in subsquent calls\");\n\t\tString spicy = \"spicy\";\n\t\twhen(dish.getSpice()).thenReturn(spicy);\t\t\n\t\tdoAnswer(new SpiceAnswer()).when(dish).eat(spicy);\n\t\tcustomer.eat(dish);\t\t\n\t\t\n\t\tspicy = \"too spicy\";\n\t\twhen(dish.getSpice()).thenReturn(spicy);\t\t\n\t\tdoAnswer(new SpiceAnswer()).when(dish).eat(spicy);\n\t\tcustomer.eat(dish);\t\t\n\t}\n\t\n\tprivate class SpiceAnswer implements Answer {\n\t\t@Override\n\t\tpublic String answer(InvocationOnMock invocation) throws Throwable {\n\t\t\tString arg = (String) invocation.getArguments()[0];\n\t\t\tif (\"too spicy\".equals(arg)) {\n\t\t\t\tthrow new RuntimeException(\"Spicy dish!\");\n\t\t\t}\n\t\t\treturn arg;\n\t\t}\t\t\n\t}\n\t\n\t@Test\n\tpublic void eatMultipleDishes() throws WrongDishException {\n\t\tSystem.out.println(\"Train dish to not throw NotSoTastyException when called first time and return in subsequent calls\");\n\t\tdoThrow(NotSoTastyException.class).doNothing().when(dish).eat();\n\t\ttry {\n\t\t\tcustomer.eat(dish);\n\t\t\tAssert.fail(\"allows eating, should have failed with NotSoTastyException\");\n\t\t} catch(NotSoTastyException e) {\n\t\t\tSystem.out.println(\"Coudln't eat the dish, not very tasty\");\n\t\t}\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\n\t\tcustomer.eat(dish);\n\t\tSystem.out.println(\"Finished the dish, no exception thrown\");\t\t\n\t}\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output:<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Train dish to not throw NotSoTastyException when called first time and return in subsequent calls\nTaste the food\nNot very tasty\nCoudln't eat the dish, not very tasty\nTaste the food\nAte the food\nFinished the dish, no exception thrown\nTaste the food\nAte the food\nFinished the dish, no exception thrown\nPASSED: eatMultipleDishes\n<\/pre>\n<h2>7. Download the Eclipse Project<\/h2>\n<p>This was an example of Mockito void Method.<\/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\/mockitoVoid.zip\"><strong>mockitoVoid.zip<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Mockito Hello World Example, we have learnt how to stub a non-void method that returns something. Sometimes we may also need to stub a void method which is what I am going to show in this article. Before I start with my example, a bit about my setup: I am using&nbsp;Maven&nbsp;\u2013 the&nbsp;build tool Eclipse &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-21481","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 void Method Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In Mockito Hello World Example, we have learnt how to stub a non-void method that returns something. Sometimes we may also need to stub a void method\" \/>\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-void-method-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mockito void Method Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In Mockito Hello World Example, we have learnt how to stub a non-void method that returns something. Sometimes we may also need to stub a void method\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-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-23T09:00:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-09T10:37:35+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=\"9 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-void-method-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/\"},\"author\":{\"name\":\"Ram Mokkapaty\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/7b1823eb5bd673bd375f8bee33b70cd8\"},\"headline\":\"Mockito void Method Example\",\"datePublished\":\"2015-03-23T09:00:58+00:00\",\"dateModified\":\"2019-04-09T10:37:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/\"},\"wordCount\":548,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-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-void-method-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/\",\"name\":\"Mockito void Method Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"datePublished\":\"2015-03-23T09:00:58+00:00\",\"dateModified\":\"2019-04-09T10:37:35+00:00\",\"description\":\"In Mockito Hello World Example, we have learnt how to stub a non-void method that returns something. Sometimes we may also need to stub a void method\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-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-void-method-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 void Method 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 void Method Example - Java Code Geeks","description":"In Mockito Hello World Example, we have learnt how to stub a non-void method that returns something. Sometimes we may also need to stub a void method","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-void-method-example\/","og_locale":"en_US","og_type":"article","og_title":"Mockito void Method Example - Java Code Geeks","og_description":"In Mockito Hello World Example, we have learnt how to stub a non-void method that returns something. Sometimes we may also need to stub a void method","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-03-23T09:00:58+00:00","article_modified_time":"2019-04-09T10:37:35+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":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/"},"author":{"name":"Ram Mokkapaty","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/7b1823eb5bd673bd375f8bee33b70cd8"},"headline":"Mockito void Method Example","datePublished":"2015-03-23T09:00:58+00:00","dateModified":"2019-04-09T10:37:35+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/"},"wordCount":548,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-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-void-method-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/","name":"Mockito void Method Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","datePublished":"2015-03-23T09:00:58+00:00","dateModified":"2019-04-09T10:37:35+00:00","description":"In Mockito Hello World Example, we have learnt how to stub a non-void method that returns something. Sometimes we may also need to stub a void method","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-void-method-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-void-method-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 void Method 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\/21481","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=21481"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/21481\/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=21481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=21481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=21481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}