{"id":37477,"date":"2016-06-01T15:00:10","date_gmt":"2016-06-01T12:00:10","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=37477"},"modified":"2019-04-09T13:32:55","modified_gmt":"2019-04-09T10:32:55","slug":"mockito-tutorial-beginners","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/","title":{"rendered":"Mockito Tutorial for Beginners"},"content":{"rendered":"<p>Mocking is a testing technique widely used not only in Java, but in any other object oriented programming language, that consists in exchanging . There are several mocking testing frameworks for Java, but this tutorial will explain how to use Mockito, probably the most popular for Java language.<\/p>\n<p>For this tutorial, we will use:<\/p>\n<ul>\n<li>Java 1.7.0<\/li>\n<li>Eclipse Mars 2, release 4.5.2.<\/li>\n<li>JUnit 4.<\/li>\n<li>Mockito 1.10.19.<\/li>\n<\/ul>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#section_1\">1. What is mocking?<\/a><\/dt>\n<dd><a href=\"#section_1_1\">1.1. Why should we mock?<\/a><\/dd>\n<dt><a href=\"#section_2\">2. Project creation<\/a><\/dt>\n<dt><a href=\"#section_3\">3. Mockito installation<\/a><\/dt>\n<dd><a href=\"#section_3_1\">3.1. Download the JAR<\/a><\/dd>\n<dd><a href=\"#section_3_2\">3.2. With build tools<\/a><\/dd>\n<dt><a href=\"#section_4\">4. Base code to test<\/a><\/dt>\n<dt><a href=\"#section_5\">5. Adding behavior<\/a><\/dt>\n<dt><a href=\"#section_6\">6. Verifying behavior<\/a><\/dt>\n<dd><a href=\"#section_6_1\">6.1. Verify that method has been called<\/a><\/dd>\n<dd><a href=\"#section_6_2\">6.2. Verify that method has been called <em>n<\/em> times<\/a><\/dd>\n<dd><a href=\"#section_6_3\">6.3. Verify method call order<\/a><\/dd>\n<dd><a href=\"#section_6_4\">6.4. Verification with timeout<\/a><\/dd>\n<dt><a href=\"#section_7\">7. Throwing exceptions<\/a><\/dt>\n<dt><a href=\"#section_8\">8. Shorthand mock creation<\/a><\/dt>\n<dt><a href=\"#section_9\">9. Mocking <em>void<\/em> returning methods<\/a><\/dt>\n<dt><a href=\"#section_10\">10. Mocking real objects: @Spy<\/a><\/dt>\n<dt><a href=\"#section_11\">11. Summary<\/a><\/dt>\n<dt><a href=\"#section_12\">12. Download the Eclipse Project<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2 id=\"section_1\">1. What is mocking?<\/h2>\n<p>Mocking is a testing technique where real components are replaced with objects that have a predefined behavior (mock objects) only for the test\/tests that have been created for. In other words, a mock object is an object that is configured to return a specific output for a specific input, without performing any real action.<\/p>\n<h3 id=\"section_1_1\">1.1. Why should we mock?<\/h3>\n<p>If we start mocking wildly, without understanding why mocking is important and how can it help us, we will probably put on doubt the usefulness of mocking.<\/p>\n<p>There are several scenarios where we should use mocks:<\/p>\n<ul>\n<li>When we want to <strong>test a component that depends on other component, but which is not yet developed<\/strong>. This happens often when working in team, and component development is divided between several developers. If mocking wouldn&#8217;t exist, we would have to wait until the other developer\/developers end the required component\/component for testing ours.<\/li>\n<li>When the <strong>real component performs slow operations<\/strong>, usual with dealing with database connections or other intense disk read\/write operations. It is not weird to face database queries that can take 10, 20 or more seconds in production environments. Forcing our tests to wait that time would be a considerable waste of useful time that can be spent in other important parts of the development.<\/li>\n<li>When there are <strong>infrastructure concerns that would make impossible the testing<\/strong>. This is similar in same way to the first scenario described when, for example, our development connects to a database, but the server where is hosted is not configured or accessible for some reason.<\/li>\n<\/ul>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip project creation and Mockito installation, and jump directly to the <a href=\"#section_4\"><strong>beginning of the tutorial<\/strong><\/a> below.<\/div>\n<h2 id=\"section_2\">2. Project creation<\/h2>\n<p>Go to &#8220;File\/New\/Java Project&#8221;. You will be asked to enter a name for the project. Then, <strong>press &#8220;Next&#8221;, not &#8220;Finish&#8221;<\/strong>.<\/p>\n<p>In the new window that has appeared, go to &#8220;Libraries&#8221; tab, select &#8220;Add library&#8221; button, and then select &#8220;JUnit&#8221;, as shown in the following images below:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/05\/1-Add-JUnit-1.jpg\" width=\"539\" height=\"826\"><\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/05\/2-Add-JUnit-2.jpg\" width=\"525\" height=\"499\"><\/p>\n<p>You can now finish the project creation.<\/p>\n<h2 id=\"section_3\">3. Mockito installation<\/h2>\n<h3 id=\"section_3_1\">3.1. Download the JAR<\/h3>\n<ul>\n<li>Download Mockito JAR file from <a href=\"http:\/\/mvnrepository.com\/artifact\/org.mockito\/mockito-all\/1.10.19\">Maven Repository<\/a>.<\/li>\n<li>Place it inside your working directory, for example, in a lib directory in the directory root.<\/li>\n<li>Refresh the Package Explorer in Eclipse (F5).<\/li>\n<li>Now, a new lib directory should be displayed, with the Mockito JAR file inside it. Right click on it an select &#8220;Build Path\/Add to Build Path&#8221; (shown in image below).<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/05\/3-Add-mockito-JAR-to-buildpath.jpg\"><\/p>\n<h3 id=\"section_3_2\">3.2. With build tools<\/h3>\n<h4>3.2.1. Maven<\/h4>\n<p>Just declare the dependency as it follows:<\/p>\n<pre class=\"brush:xml\">&lt;dependency&gt;\n&nbsp;&nbsp;&nbsp; &lt;groupId&gt;org.mockito&lt;\/groupId&gt;\n&nbsp;&nbsp;&nbsp; &lt;artifactId&gt;mockito-all&lt;\/artifactId&gt;\n&nbsp;&nbsp;&nbsp; &lt;version&gt;1.10.19&lt;\/version&gt;\n&lt;\/dependency&gt;<\/pre>\n<h4>3.2.2. Gradle<\/h4>\n<p>Declare the dependency as it is shown below:<\/p>\n<pre class=\"brush:bash\">repositories {\n&nbsp;&nbsp;&nbsp; jcenter()\n}\n\ndependencies {\n&nbsp;&nbsp;&nbsp; testCompile \"org.mockito:mockito-core:1.+\"\n}<\/pre>\n<h2 id=\"section_4\">4. Base code to test<\/h2>\n<p>Let&#8217;s suppose that our application is for authenticating users, and that our job is to develop the interface that the final user will use, and that developing the logic is someone else&#8217;s job. For mocking, is indispensable to agree the interfaces to mock, that is, the method definitions: name, parameters, and return type. For this case, the agreed interface will be a public method <code>authenticateUser<\/code>, that receives two strings, the user name and the password; returning a boolean indicating if the authentication succeeded or not. So, the interface would be the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorInterface.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockitotutorial.basecode;\n\npublic interface AuthenticatorInterface {\n\n&nbsp;&nbsp;&nbsp; \/**\n&nbsp;&nbsp;&nbsp;&nbsp; * User authentication method definition.\n&nbsp;&nbsp;&nbsp;&nbsp; *\n&nbsp;&nbsp;&nbsp;&nbsp; * @param username The user name to authenticate.\n&nbsp;&nbsp;&nbsp;&nbsp; * @param password The password to authenticate the user.\n&nbsp;&nbsp;&nbsp;&nbsp; * @return True if the user has been authenticated; false if it has not.\n&nbsp;&nbsp;&nbsp;&nbsp; * @throws EmptyCredentialsException If the received credentials (user name, password) are\n&nbsp;&nbsp;&nbsp;&nbsp; * empty.\n&nbsp;&nbsp;&nbsp;&nbsp; *\/\n&nbsp;&nbsp; &nbsp;public boolean authenticateUser(String username, String password);\n\n}<\/pre>\n<p>And the source that uses this interface:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplication.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockitotutorial.basecode;\n\npublic class AuthenticatorApplication {\n\n&nbsp;&nbsp; &nbsp;private AuthenticatorInterface authenticator;\n\n&nbsp;&nbsp;&nbsp; \/**\n&nbsp;&nbsp; &nbsp; * AuthenticatorApplication constructor.\n&nbsp;&nbsp; &nbsp; *\n&nbsp;&nbsp; &nbsp; * @param authenticator Authenticator interface implementation.\n&nbsp;&nbsp; &nbsp; *\/\n&nbsp;&nbsp; &nbsp;public AuthenticatorApplication(AuthenticatorInterface authenticator) {\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; this.authenticator = authenticator;\n&nbsp;&nbsp; &nbsp;}\n\n&nbsp;&nbsp;&nbsp; \/**\n&nbsp;&nbsp; &nbsp; * Tries to authenticate an user with the received user name and password, with the received\n&nbsp;&nbsp; &nbsp; * AuthenticatorInterface interface implementation in the constructor.\n&nbsp;&nbsp; &nbsp; *\n&nbsp;&nbsp; &nbsp; * @param username The user name to authenticate.\n&nbsp;&nbsp; &nbsp; * @param password The password to authenticate the user.\n&nbsp;&nbsp; &nbsp; * @return True if the user has been authenticated; false if it has not.\n     *\/\n&nbsp;&nbsp; &nbsp;public boolean authenticate(String username, String password) {\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; boolean authenticated;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; authenticated = this.authenticator.authenticateUser(username, password);\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; return authenticated;\n&nbsp;&nbsp; &nbsp;}\n}<\/pre>\n<p>We will suppose that this piece of code also implements the <code>main<\/code> method, but is not important for this example.<\/p>\n<p>Now, we are going to code the tests for <code>AuthenticatorApplication<\/code>. The testing method returns a boolean, so we will code tests for covering both possible cases: failed login, and succeeded one.<\/p>\n<p>As the code that handles the authentication is not developed, we have to make some suppositions. We are not doing any real authentication. We have to define for which values the function will succeed, and for which not.<\/p>\n<h2 id=\"section_5\">5. Adding behavior<\/h2>\n<p>Let&#8217;s see how we can mock the Authenticator:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[4,7,18,21,22,24]\">package com.javacodegeeks.mockitotutorial.basecode;\n\nimport org.junit.Test;\nimport org.mockito.Mockito;\n\nimport static org.junit.Assert.*;\nimport static org.mockito.Mockito.*;\n\npublic class AuthenticatorApplicationTest {\n\n&nbsp;&nbsp;&nbsp; @Test\n&nbsp;&nbsp;&nbsp; public void testAuthenticate() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthenticatorInterface authenticatorMock;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthenticatorApplication authenticator;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String username = \"JavaCodeGeeks\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String password = \"unsafePassword\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; authenticatorMock = Mockito.mock(AuthenticatorInterface.class);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; authenticator = new AuthenticatorApplication(authenticatorMock);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; when(authenticatorMock.authenticateUser(username, password))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .thenReturn(false);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean actual = authenticator.authenticate(username, password);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assertFalse(actual);\n&nbsp;&nbsp;&nbsp; }\n}<\/pre>\n<p>Let&#8217;s see carefully what we are doing:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<ul>\n<li>We import the required stuff, as in lines 4 and 7. The IDE will help us to do it.<\/li>\n<li>We define the mock object, in line 18. This is how the mock &#8220;learns&#8221; the method definitions to mock.<\/li>\n<li>The key part is when we <strong>add the behavior<\/strong>, as in lines 21 and 22, with the <code>when()<\/code> and <code>thenReturn()<\/code> functions. Is quite expressive: &#8220;<em><strong>When<\/strong> the mock object is called for this method with this parameters, <strong>then<\/strong> it returns this value<\/em>&#8220;. Note that we are <strong>defining the behavior in the mock object, not to the class calling the mock object<\/strong>.<\/li>\n<\/ul>\n<p>As we are adding the behavior to the reference that has been passed to <code>AuthenticatorApplication<\/code> instance, it doesn&#8217;t matter if we first add the behavior and then we pass the reference, or reverse.<\/p>\n<p>When the <code>AuthenticatorApplication<\/code> calls to its <code>AuthenticatorInterface<\/code>, it won&#8217;t know what is actually happening, the only thing it knows is just how to deal with the defined interface, which for this case has been designed to return <code>false<\/code> when it receives <code>\"JavaCodeGeeks\"<\/code> and <code>\"unsafePassword\"<\/code> as inputs.<\/p>\n<h2 id=\"section_6\">6. Verifying behavior<\/h2>\n<p>Mockito allows to make several verifications about our mock objects. Let&#8217;s see which are they.<\/p>\n<h3 id=\"section_6_1\">6.1. Verify that method has been called<\/h3>\n<p>We can check if a method has been called with certain parameters. For that, we would do something similar to the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ ...\n\nverify(authenticatorMock).authenticateUser(username, password);\n\n\/\/ ...<\/pre>\n<p>To verify that <code>authenticatorMock<\/code> mock&#8217;s&nbsp;<code>authenticateUser<\/code> method, with&nbsp;<code>username<\/code> and <code>password<\/code> parameters.<\/p>\n<p>Of course, this verification only makes sense if we make it after the call is supposed to be done.<\/p>\n<p>Apart from checking that the method is actually being called, this verifications are <strong>useful to check that the parameters arrive to the method call as they are supposed to arrive<\/strong>. So, for example, if you run the test with the following verification:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ ...\n\nverify(authenticatorMock).authenticateUser(username, \"not the original password\");\n\n\/\/ ...<\/pre>\n<p>The test will fail.<\/p>\n<h3 id=\"section_6_2\">6.2. Verify that method has been called <em>n<\/em> times<\/h3>\n<p>Apart from checking that the method has been called or not, we have many possibilities regarding to number of method calls. Let&#8217;s see how we can do it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ ...\n\nverify(authenticatorMock, times(1)).authenticateUser(username, password);\nverify(authenticatorMock, atLeastOnce()).authenticateUser(username, password);\nverify(authenticatorMock, atLeast(1)).authenticateUser(username, password);\nverify(authenticatorMock, atMost(1)).authenticateUser(username, password);\n\n\/\/ ...<\/pre>\n<p>As you can see, we have different notations available to make the verifications: specifying the number of times that the mocking method should be called, how much times should be called at least, and how much at most.<\/p>\n<p>As in the previous example, <strong>the verifications are made for the exact parameters that the mocking method uses<\/strong>.<\/p>\n<p>We can also verify that the method has never been called:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ ...\n\nverify(authenticatorMock, never()).authenticateUser(username, password); \/\/ This will make the test fail!\n\n\/\/ ...<\/pre>\n<p>Which, actually, is equivalent to <code>times(0)<\/code>, but would be more expressive when we really want to verify that a method has never been called.<\/p>\n<h3 id=\"section_6_3\">6.3. Verify method call order<\/h3>\n<p>We can also verify in which order have been executed the mock methods.<\/p>\n<p>To see how it works, let&#8217;s add a dummy method in the interface:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorInterface.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ ...\n\npublic void foo();\n\n\/\/ ...<\/pre>\n<p>And also call it from the original&nbsp;<code>AuthenticatorApplication.authenticate()<\/code>&nbsp; method:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplication.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[6,7]\">\/\/ ...\n\npublic boolean authenticate(String username, String password) throws EmptyCredentialsException{\n&nbsp;&nbsp;&nbsp; boolean authenticated;\n\n    this.authenticator.foo();\n&nbsp;&nbsp;&nbsp; authenticated = this.authenticator.authenticateUser(username, password);\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp; return authenticated;\n}\n\n\/\/ ...<\/pre>\n<p>Now, let&#8217;s see how we would verify that the <code>foo()<\/code> method is called before&nbsp;<code>authenticateUser()<\/code> method:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ ...\n\nInOrder inOrder = inOrder(authenticatorMock);\ninOrder.verify(authenticatorMock).foo();\ninOrder.verify(authenticatorMock).authenticateUser(username, password);\n\n\/\/ ...<\/pre>\n<p>We just have to create an&nbsp;<code>InOrder<\/code> instance for the mock object to make the verification, and then call its&nbsp;<code>verify()<\/code> method in the same order we want to make the verification. So, the following snippet, for the current <code>AuthenticatorApplication.authenticate()<\/code> method, will make the test fail:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ ...\n\nInOrder inOrder = inOrder(authenticatorMock);\ninOrder.verify(authenticatorMock).authenticateUser(username, password); \/\/ This will make the test fail!\ninOrder.verify(authenticatorMock).foo();\n\n\/\/ ...<\/pre>\n<p>Because in the method the mocking object is used, <code>authenticateUser()<\/code> is called after <code>foo()<\/code>.<\/p>\n<h3 id=\"section_6_4\">6.4. Verification with timeout<\/h3>\n<p>Mockito verification also allows to specify a timeout for the mock methods execution. So, if we want to ensure that our <code>authenticateUser()<\/code> method runs in, for example, 100 milliseconds or less, we would do the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ ...\n\nverify(authenticatorMock, timeout(100)).authenticateUser(username, password);\n\n\/\/ ...<\/pre>\n<p>The timeout verification can be combined with the method call, so, we could verify the timeout for <em>n<\/em> method calls:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ ...\n\nverify(authenticatorMock, timeout(100).times(1)).authenticateUser(username, password);\n\n\/\/ ...<\/pre>\n<p>And any other method call verifier.<\/p>\n<h2 id=\"section_7\">7. Throwing exceptions<\/h2>\n<p>Mockito allows its mocks to throw exceptions. Is possible to make a mock method throw an exception that is not defined in the method signature, but is better to agree in a common method definition from the beginning, including exception throwing.<\/p>\n<p>We could create an exception class to be thrown when, for example, empty credentials are provided:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>EmptyCredentialsException.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockitotutorial.basecode;\n\npublic class EmptyCredentialsException extends Exception {\n&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp; public EmptyCredentialsException() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(\"Empty credentials!\");\n&nbsp;&nbsp;&nbsp; }\n}<\/pre>\n<p>We add it to the method signature of our <code>AuthenticatorInterface<\/code>, and also to its call in <code>AuthenticatorApplication<\/code>:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorInterface.java<br \/>\n<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockitotutorial.basecode;\n\npublic interface AuthenticatorInterface {\n\n&nbsp;&nbsp;&nbsp; \/**\n&nbsp;&nbsp;&nbsp;&nbsp; * User authentication method definition.\n&nbsp;&nbsp;&nbsp;&nbsp; *\n&nbsp;&nbsp;&nbsp;&nbsp; * @param username The user name to authenticate.\n&nbsp;&nbsp;&nbsp;&nbsp; * @param password The password to authenticate the user.\n&nbsp;&nbsp;&nbsp;&nbsp; * @return True if the user has been authenticated; false if it has not.\n&nbsp;&nbsp;&nbsp;&nbsp; * @throws EmptyCredentialsException If the received credentials (user name, password) are\n&nbsp;&nbsp;&nbsp;&nbsp; * empty.\n&nbsp;&nbsp;&nbsp;&nbsp; *\/\n&nbsp;&nbsp; &nbsp;public boolean authenticateUser(String username, String password) throws EmptyCredentialsException;\n\n}<\/pre>\n<p>For the test, we will create another test case for expecting the exception:[ulp id=&#8217;GhnY46zldrhgoeym&#8217;]<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[11,12]\">\/\/ ...\n\n@Test (expected = EmptyCredentialsException.class)\npublic void testAuthenticateEmptyCredentialsException() throws EmptyCredentialsException {\n&nbsp;&nbsp;&nbsp; AuthenticatorInterface authenticatorMock;\n&nbsp;&nbsp;&nbsp; AuthenticatorApplication authenticator;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp; authenticatorMock = Mockito.mock(AuthenticatorInterface.class);\n&nbsp;&nbsp;&nbsp; authenticator = new AuthenticatorApplication(authenticatorMock);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp; when(authenticatorMock.authenticateUser(\"\", \"\"))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .thenThrow(new EmptyCredentialsException());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp; authenticator.authenticate(\"\", \"\");\n}<\/pre>\n<p>As you can see, is almost identical to adding return values to the mock. The only difference is that we have to call <code>thenThrow()<\/code>, passing the exception instance we want to be thrown. And, of course, we have to handle the exception; in this case, we have used the <code>expected<\/code> rule to &#8220;assert&#8221; the exception.<\/p>\n<h2 id=\"section_8\">8. Shorthand mock creation<\/h2>\n<p>For a few mocks, creating every mock object is not a problem. But, when there is a considerable number of them, it can be quite tedious to create every mock.<\/p>\n<p>Mockito provides a shorthand notation, which is really expressive, to <strong>inject the mock dependencies<\/strong>.<\/p>\n<p>If we want to inject dependencies with Mockito, we have to take the two things into account:<\/p>\n<ul>\n<li>Only works for class scope, not for function scope.<\/li>\n<li>We must run the test class with&nbsp;<code>MockitoJUnitRunner.class<\/code>.<\/li>\n<\/ul>\n<p>So, we would have to do the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[3,6,9]\">\/\/ ...\n\n@RunWith(MockitoJUnitRunner.class)\npublic class AuthenticatorApplicationTest {\n\n&nbsp;&nbsp;&nbsp; @Mock\n&nbsp;&nbsp;&nbsp; private AuthenticatorInterface authenticatorMock;\n&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp; @InjectMocks\n&nbsp;&nbsp;&nbsp; private AuthenticatorApplication authenticator;\n\n    \/\/ ...\n}<\/pre>\n<p>With the <code>@Mock<\/code> annotation, we define the dependencies to inject. And then, with <code>@InjectMocks<\/code>, we specify where to inject the defined dependencies. With only those annotations, we have an instance of <code>AuthenticatorApplication<\/code> with the&nbsp;<code>AuthenticatorInterface<\/code> injected.<\/p>\n<p>To perform the injection, Mockito tries the following ways, in order:<\/p>\n<ol>\n<li>By constructor (as we have).<\/li>\n<li>By setter.<\/li>\n<li>By class field.<\/li>\n<\/ol>\n<p>If Mockito is unable to do the injection, the result will be a null reference to the object to be injected, which in this case, would be <code>AuthenticatorApplication<\/code>.<\/p>\n<p>But, as we have a constructor where the interface is passed, Mockito is supposed to do the injection properly. So now, we could make another test case to test it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">@Test\npublic void testAuthenticateMockInjection() throws EmptyCredentialsException {\n&nbsp;&nbsp;&nbsp; String username = \"javacodegeeks\";\n&nbsp;&nbsp;&nbsp; String password = \"s4f3 p4ssw0rd\";\n\n&nbsp;&nbsp;&nbsp; when(this.authenticatorMock.authenticateUser(username, password))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .thenReturn(true);\n\n&nbsp;&nbsp;&nbsp; boolean actual = this.authenticator.authenticate(\"javacodegeeks\", \"s4f3 p4ssw0rd\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp; assertTrue(actual);\n}<\/pre>\n<p>We don&#8217;t have to do anything more than the test itself, Mockito has created an instance for the <code>AuthenticatorApplication<\/code> with the injected mock.<\/p>\n<h2 id=\"section_9\">9. Mocking <em>void<\/em> returning methods<\/h2>\n<p>In the previous examples, we have used <code>when()<\/code> for adding behavior to the mocks. But this way won&#8217;t work for methods that return <code>void<\/code>. If we try to use <code>when()<\/code> with a void method, the IDE will mark an error, and it won&#8217;t let us compile the code.<\/p>\n<p>First, we are going to change the previous example to make <code>AuthenticatorInterface<\/code> method return <code>void<\/code>, and make it throw an exception if the user has not been successfully authenticated, to give sense to the&nbsp;<code>void<\/code> return. We are going to create another package <code>com.javacodegeeks.mockitotutorial.voidmethod<\/code>, not to modify the previous working code.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorInterface.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockitotutorial.voidmethod;\n\npublic interface AuthenticatorInterface {\n\n&nbsp;&nbsp;&nbsp; \/**\n&nbsp;&nbsp;&nbsp;&nbsp; * User authentication method definition.\n&nbsp;&nbsp;&nbsp;&nbsp; *\n&nbsp;&nbsp;&nbsp;&nbsp; * @param username The user name to authenticate.\n&nbsp;&nbsp;&nbsp;&nbsp; * @param password The password to authenticate the user.\n&nbsp;&nbsp;&nbsp;&nbsp; * @throws NotAuthenticatedException If the user can't be authenticated.\n&nbsp;&nbsp;&nbsp;&nbsp; *\/\n&nbsp;&nbsp; &nbsp;public void authenticateUser(String username, String password) throws NotAuthenticatedException;\n\n}<\/pre>\n<p>And also, its call:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplication.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockitotutorial.voidmethod;\n\npublic class AuthenticatorApplication {\n\n&nbsp;&nbsp; &nbsp;private AuthenticatorInterface authenticator;\n\n&nbsp;&nbsp;&nbsp; \/**\n&nbsp;&nbsp;&nbsp;&nbsp; * AuthenticatorApplication constructor.\n&nbsp;&nbsp;&nbsp;&nbsp; *\n&nbsp;&nbsp;&nbsp;&nbsp; * @param authenticator Authenticator interface implementation.\n&nbsp;&nbsp;&nbsp;&nbsp; *\/\n&nbsp;&nbsp; &nbsp;public AuthenticatorApplication(AuthenticatorInterface authenticator) {\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; this.authenticator = authenticator;\n&nbsp;&nbsp; &nbsp;}\n\n&nbsp;&nbsp;&nbsp; \/**\n&nbsp;&nbsp;&nbsp;&nbsp; * Tries to authenticate an user with the received user name and password, with the received\n&nbsp;&nbsp;&nbsp;&nbsp; * AuthenticatorInterface interface implementation in the constructor.\n&nbsp;&nbsp;&nbsp;&nbsp; *\n&nbsp;&nbsp;&nbsp;&nbsp; * @param username The user name to authenticate.\n&nbsp;&nbsp;&nbsp;&nbsp; * @param password The password to authenticate the user.\n&nbsp;&nbsp;&nbsp;&nbsp; * @throws NotAuthenticatedException If the user can't be authenticated.\n&nbsp;&nbsp;&nbsp;&nbsp; *\/\n&nbsp;&nbsp; &nbsp;public void authenticate(String username, String password) throws NotAuthenticatedException {\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; this.authenticator.authenticateUser(username, password);\n&nbsp;&nbsp; &nbsp;}\n}<\/pre>\n<p>The required exception class also:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>NotAuthenticatedException.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.mockitotutorial.voidmethod;\n\npublic class NotAuthenticatedException extends Exception {\n&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp; public NotAuthenticatedException() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(\"Could not authenticate!\");\n&nbsp;&nbsp;&nbsp; }\n}<\/pre>\n<p>Now, to mock <code>AuthenticatorInterface.authenticateUser<\/code>, we have to use the <code>do<\/code> family methods:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AuthenticatorApplicationTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[20,21,22]\">package com.javacodegeeks.mockitotutorial.voidmethod;\n\nimport static org.mockito.Mockito.doThrow;\n\nimport org.junit.Test;\nimport org.mockito.Mockito;\n\npublic class AuthenticatorApplicationTest {\n\n&nbsp;&nbsp;&nbsp; @Test(expected = NotAuthenticatedException.class)\n&nbsp;&nbsp;&nbsp; public void testAuthenticate() throws NotAuthenticatedException {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthenticatorInterface authenticatorMock;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthenticatorApplication authenticator;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String username = \"JavaCodeGeeks\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String password = \"wrong password\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; authenticatorMock = Mockito.mock(AuthenticatorInterface.class);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; authenticator = new AuthenticatorApplication(authenticatorMock);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; doThrow(new NotAuthenticatedException())\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .when(authenticatorMock)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .authenticateUser(username, password);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; authenticator.authenticate(username, password);\n&nbsp;&nbsp;&nbsp; }\n}<\/pre>\n<p>We are doing the same thing as in the previous example, but using a different notation (lines 20, 21, 22). We could say that it&#8217;s almost the same syntax, but inverted: first, we add the behavior (a <code>throw<\/code> behavior); and then, we specify the method we are adding the behavior to.<\/p>\n<h2 id=\"section_10\">10. Mocking real objects: @Spy<\/h2>\n<p>Exists the possibility of creating mocks that wrap objects, i.e., instances of implemented classes. This is called &#8220;spying&#8221; by Mockito.<\/p>\n<p>When you call the method of a spied object, the real method will be called, unless a predefined behavior was defined.<\/p>\n<p>Let&#8217;s create a new test case in a new package to see how it works:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>SpyExampleTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[15]\">package com.javacodegeeks.mockitotutorial.spy;\n\nimport static org.mockito.Mockito.*;\n\nimport java.util.HashMap;\nimport java.util.Map;\n\nimport org.junit.Test;\n\npublic class SpyExampleTest {\n\n&nbsp;&nbsp;&nbsp; @Test\n&nbsp;&nbsp;&nbsp; public void spyExampleTest() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Map&lt;String, String&gt; hashMap = new HashMap&lt;String, String&gt;();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Map&lt;String, String&gt; hashMapSpy = spy(hashMap);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(hashMapSpy.get(\"key\")); \/\/ Will print null.\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hashMapSpy.put(\"key\", \"A value\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(hashMapSpy.get(\"key\")); \/\/ Will print \"A value\".\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; when(hashMapSpy.get(\"key\")).thenReturn(\"Another value\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(hashMapSpy.get(\"key\")); \/\/ Will print \"Another value\".\n&nbsp;&nbsp;&nbsp; }\n}<\/pre>\n<p>As you can see, we can both delegate the method call to the real implementation, or define a behavior.<\/p>\n<p>You might think that this is a quite odd feature. And you&#8217;ll probably right. In fact, <strong>Mockito documentation recommends to use this only occasionally<\/strong>.<\/p>\n<h2 id=\"section_11\">11. Summary<\/h2>\n<p>This tutorial has explained what mocking is, and how to put in practice this technique in Java with Mockito framework. We have seen how to add predefined behaviors to our mock objects, and several ways of verifying that those mock objects behave as they are supposed to do. We also have seen the possibility of mocking real objects, a feature that should be used carefully.<\/p>\n<h2 id=\"section_12\">12. Download the Eclipse Project<\/h2>\n<p>This was a tutorial of Mockito.<\/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\/2016\/05\/MockitoTutorialForBeginners.zip\"><strong>MockitoTutorialForBeginners<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Mocking is a testing technique widely used not only in Java, but in any other object oriented programming language, that consists in exchanging . There are several mocking testing frameworks for Java, but this tutorial will explain how to use Mockito, probably the most popular for Java language. For this tutorial, we will use: Java &hellip;<\/p>\n","protected":false},"author":97,"featured_media":21338,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[923],"tags":[1011],"class_list":["post-37477","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mockito","tag-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Mockito Tutorial for Beginners - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Mocking is a testing technique widely used not only in Java, but in any other object oriented programming language, that consists in exchanging . There\" \/>\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-tutorial-beginners\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mockito Tutorial for Beginners - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Mocking is a testing technique widely used not only in Java, but in any other object oriented programming language, that consists in exchanging . There\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/\" \/>\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=\"2016-06-01T12:00:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-09T10:32:55+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=\"toni.23\" \/>\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=\"toni.23\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 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-tutorial-beginners\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/\"},\"author\":{\"name\":\"toni.23\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/c77a324672dcd74b5c861c9af5308228\"},\"headline\":\"Mockito Tutorial for Beginners\",\"datePublished\":\"2016-06-01T12:00:10+00:00\",\"dateModified\":\"2019-04-09T10:32:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/\"},\"wordCount\":2130,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"keywords\":[\"test\"],\"articleSection\":[\"Mockito\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/\",\"name\":\"Mockito Tutorial for Beginners - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"datePublished\":\"2016-06-01T12:00:10+00:00\",\"dateModified\":\"2019-04-09T10:32:55+00:00\",\"description\":\"Mocking is a testing technique widely used not only in Java, but in any other object oriented programming language, that consists in exchanging . There\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#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-tutorial-beginners\/#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 Tutorial for Beginners\"}]},{\"@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\/c77a324672dcd74b5c861c9af5308228\",\"name\":\"toni.23\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"caption\":\"toni.23\"},\"description\":\".\",\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/julen-pardo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mockito Tutorial for Beginners - Java Code Geeks","description":"Mocking is a testing technique widely used not only in Java, but in any other object oriented programming language, that consists in exchanging . There","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-tutorial-beginners\/","og_locale":"en_US","og_type":"article","og_title":"Mockito Tutorial for Beginners - Java Code Geeks","og_description":"Mocking is a testing technique widely used not only in Java, but in any other object oriented programming language, that consists in exchanging . There","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-06-01T12:00:10+00:00","article_modified_time":"2019-04-09T10:32:55+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":"toni.23","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"toni.23","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/"},"author":{"name":"toni.23","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/c77a324672dcd74b5c861c9af5308228"},"headline":"Mockito Tutorial for Beginners","datePublished":"2016-06-01T12:00:10+00:00","dateModified":"2019-04-09T10:32:55+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/"},"wordCount":2130,"commentCount":4,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","keywords":["test"],"articleSection":["Mockito"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/","name":"Mockito Tutorial for Beginners - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","datePublished":"2016-06-01T12:00:10+00:00","dateModified":"2019-04-09T10:32:55+00:00","description":"Mocking is a testing technique widely used not only in Java, but in any other object oriented programming language, that consists in exchanging . There","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-tutorial-beginners\/#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-tutorial-beginners\/#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 Tutorial for Beginners"}]},{"@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\/c77a324672dcd74b5c861c9af5308228","name":"toni.23","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","caption":"toni.23"},"description":".","url":"https:\/\/examples.javacodegeeks.com\/author\/julen-pardo\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/37477","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\/97"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=37477"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/37477\/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=37477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=37477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=37477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}