{"id":44886,"date":"2015-11-15T15:21:47","date_gmt":"2015-11-15T13:21:47","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=44886"},"modified":"2023-12-13T11:06:36","modified_gmt":"2023-12-13T09:06:36","slug":"getting-started-with-mockito","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html","title":{"rendered":"Getting started with Mockito"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/testing-with-mockito\/\">Testing with Mockito<\/a>.<\/p>\n<p>In this course, you will dive into the magic of Mockito. You will learn about Mocks, Spies and Partial Mocks, and their corresponding Stubbing behaviour. You will also see the process of Verification with Test Doubles and Object Matchers. Finally, Test Driven Development (TDD) with Mockito is discussed in order to see how this library fits in the concept of TDD. Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/testing-with-mockito\/\">here<\/a>!<\/em><\/p>\n<div class=\"toc\">\n<h4>Table of Contents<\/h4>\n<dl>\n<dt><a href=\"#why_mock\">1. Why Mock?<\/a><\/dt>\n<dt><a href=\"#introduction\">2. Introduction to the Mockito Framework<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#stubbing_example\">2.1. A Quick Example of Stubbing<\/a><\/dt>\n<dt><a href=\"#verification_example\">2.2. A Quick Example of Verification<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#mixing\">3. Mixing up a Mockito<\/a><\/dt>\n<dt><a href=\"#junit\">4. Using Mockito with JUnit<\/a><\/dt>\n<dt><a href=\"#best_practices\">5. Mockito Best Practices<\/a><\/dt>\n<\/dl>\n<\/div>\n<p>In this tutorial we are going to look at the Mockito Mocking Framework and prepare an Eclipse project to use it by adding it to the classpath.<\/p>\n<h2><a name=\"why_mock\"><\/a>1. Why Mock?<\/h2>\n<p>All of the code we write has a network of interdependencies, it may call into the methods of several other classes which in turn may call yet other methods; indeed this is the intent and power of object oriented programming. Usually at the same time as writing our feature code we will also write test code in the form of automated unit tests. We use these unit tests to verify the behaviour of our code, to ensure that it behaves as we expect it to behave.<\/p>\n<p>When we unit test our code we want to test it in isolation and we want to test it fast. For the purposes of the unit test we only care about verifying our own code, in the current class under test. Generally we also want to execute our unit tests very regularly, perhaps more than several times per hour when we are refactoring and we are working in our continuous integration environment.<\/p>\n<p>This is when all our interdependencies become an issue. We might end up executing code in another class that has a bug that causes our unit test to fail. Imagine a class which we use to read user details from a database, what happens if there\u2019s no database present when we want to run our unit tests? Imagine a class which calls several remote web services, what if they\u2019re down or take a long time to respond? Our unit tests could fail due to our dependencies and not because of some issue with the behaviour of our code. This is undesirable.<\/p>\n<p>In addition to this, it might be very difficult to force a specific event or error condition that we want to ensure our code handles correctly. What if we want to test that some class which deserializes an object handles a possible ObjectStreamException properly? What if we want to test all boundary return values from a collaborator? What about ensuring that some calculated value is passed correctly to a collaborator? It might take a lot of coding and a long time to replicate the conditions for our tests, if it is even possible at all.<\/p>\n<p>All these issues simply disappear if we use mocks. Mocks act like a substitute for the classes with which we are collaborating, they take their place and behave exactly how we tell them to behave. Mocks let us pretend that our real collaborators are there, even though they aren\u2019t. More importantly mocks can be programmed to return whatever values we want and confirm whatever values are passed to them. Mocks execute instantly and don\u2019t require any external resources. Mocks will return what we tell them to return, throw whatever exceptions we want them to throw and will do these things over and over, on demand. They let us test only the behaviour of our own code, to ensure that our class works, regardless of the behaviour of its collaborators.<\/p>\n<p>There are several mocking frameworks available for Java, each have their own syntax, their own strengths, their own weaknesses. In this tutorial we will be using the Mockito framework, which is one of the more popular mocking frameworks available.<\/p>\n<h2><a name=\"introduction\"><\/a>2. Introduction to the Mockito Framework<\/h2>\n<p>Mockito is a Mocking Framework that makes it very easy to create mocks for the classes and interfaces with which your class under test interacts. Mockito provides a very simple API for creating mocks and assigning their behaviour. It allows you to very quickly specify expected behaviour and verify interactions with your mocks.<\/p>\n<p>Mockito has essentially two phases, one or both of which are executed as part of unit tests:<\/p>\n<ul>\n<li>Stubbing<\/li>\n<li>Verification<\/li>\n<\/ul>\n<p>Stubbing is the process of specifying the behaviour of our mocks. It is how we tell Mockito what we want to happen when we interact with our mocks. Stubbing lets us address some of the concerns which we outlined in the first section &#8211; it makes it simple to create all the possible conditions for our tests. It let\u2019s us control the responses of our mocks, including forcing them to return any value we want, or throw any exception we want. It allows us to code different behaviours under different conditions. Stubbing lets us control exactly what the mock will do.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Verification is the process of verifying interactions with our mocks. It lets us determine how our mocks were called, and how many times. It lets us look at the arguments of our mocks to make sure they are as expected. Verification lets us address the other concerns mentioned in the first section &#8211; it lets us ensure that exactly the values we expect are passed to our collaborators, and that nothing unexpected happens. Verification lets us determine exactly what happened to the mock.<\/p>\n<p>By tying together these two simple phases we can build extremely flexible and powerful unit tests, encoding complex mock behaviour and complex mock interaction verification with the very simple Mockito API.<\/p>\n<p>Mockito does have some limitations, however, including<\/p>\n<ul>\n<li>You can\u2019t mock final classes<\/li>\n<li>You can\u2019t mock static methods<\/li>\n<li>You can\u2019t mock final methods<\/li>\n<li>You can\u2019t mock equals() or hashCode()<\/li>\n<\/ul>\n<h3><a name=\"stubbing_example\"><\/a>2.1. A Quick Example of Stubbing<\/h3>\n<p>Imagine you are writing a class which calls the API of a physical temperature sensor. You want to call the <code>double getDegreesC()<\/code> method and return one of the following Strings &#8211; \u201cHot\u201d, \u201cMild\u201d, \u201cCold\u201d &#8211; based on the value returned from the sensor. It would be very difficult, to say the least, to have your unit tests control the environmental temperature of the room in order to test your functionality. But what if we use Mockito to create a mock which we substitute for the sensor?<\/p>\n<p>Now we can write code like this in our unit test:<\/p>\n<pre class=\"brush:java\">\nwhen(sensor.getDegreesC()).thenReturn(15.0);\n<\/pre>\n<p>This tells Mockito that when the mock sensor receives a call to <code>getDegreesC()<\/code> it should then return the value 15.0.<\/p>\n<h3><a name=\"verification_example\"><\/a>2.2. A Quick Example of Verification<\/h3>\n<p>Imagine you have a class which does some calculation and is responsible for notifying an observer when it has finished it\u2019s calculation. You want to make sure that the observer\u2019s <code>notify()<\/code> method was called once as part of your method execution. You could set some boolean in the observer and check it from your unit test, but this means altering some production code, code you might not even own. What about Mockito, what if the observer is a mock?<\/p>\n<p>Now we can write code like this in our unit test:<\/p>\n<pre class=\"brush:java\">\nverify(observer).notify();\n<\/pre>\n<p>This tells Mockito that the <code>notify()<\/code> method must be called once and only once, otherwise the unit test should fail.<\/p>\n<h2><a name=\"mixing\"><\/a>3. Mixing up a Mockito<\/h2>\n<p>Now we\u2019ve learned a bit about the framework let\u2019s use it in a project.<\/p>\n<p>If you use Maven then adding Mockito to your project is as simple as adding the following dependency:<\/p>\n<pre class=\"brush:xml\">\n&lt;dependency&gt;\n&lt;groupId&gt;org.mockito&lt;\/groupId&gt;\n&lt;artifactId&gt;mockito-all&lt;\/artifactId&gt;\n   \t&lt;version&gt;1.9.5&lt;\/version&gt;\n   \t&lt;scope&gt;test&lt;\/scope&gt;\n   &lt;\/dependency&gt;\n<\/pre>\n<p>If you use Gradle then just add the following<\/p>\n<pre class=\"brush:groovy\">\ndependencies {\n    testCompile \"org.mockito:mockito-all:1.9.5\"\n}\n<\/pre>\n<p>To add Mockito to the classpath of an Eclipse project the old fashioned way grab the latest jar from the <a href=\"https:\/\/code.google.com\/p\/mockito\/downloads\/list\">Mockito download page<\/a> (take mockito-all-1.9.5.jar) and download it to your hard drive.<\/p>\n<p>Right click on your eclipse project and select \u2018Properties\u2019 and then select \u2018Java Build Path\u2019 in the left pane and \u2018Libraries\u2019 on the right.<\/p>\n<p>On the \u2018Libraries\u2019 tab click the \u2018Add External Jars\u2019 button and navigate to the mockito-all jar you previously downloaded. Select the jar and it is now added to your project and available to use.<\/p>\n<p>As of the time of writing the latest version of Mockito is 1.9.5 but you should check for updates before you add it to your project.<\/p>\n<h2><a name=\"junit\"><\/a>4. Using Mockito with JUnit<\/h2>\n<p>To integrate Mockito into your JUnit test class you can use the provided Test Runner <code>MockitoJUnitRunner<\/code>. Just annotate your test class with:<\/p>\n<pre class=\"brush:java\">\n@RunWith(MockitoJUnitRunner.class)\n<\/pre>\n<p>This tells Mockito to take any annotated mocks within the test class and initialise them for mocking. You can then simply annotate any instance variable with <code>@Mock<\/code> to use it as a mock. Note that you should import <code>org.mockito.Mock<\/code> and not <code>org.mockito.mockitoannotations.Mock<\/code>, which is deprecated.<\/p>\n<p>As with anything a full example makes things clearer, we will create a new Test class and within it we\u2019ll use Mockito to mock a <code>java.util.List<\/code>:<\/p>\n<pre class=\"brush:java\">\nimport java.util.List;\nimport org.junit.runner.RunWith;\nimport org.mockito.Mock;\nimport org.mockito.runners.MockitoJUnitRunner;\n\n@RunWith(MockitoJUnitRunner.class)\npublic class MyTest {\n    \n    @Mock\n    private List&lt;String&gt; mockList;\n    \n}<\/pre>\n<p>The <code>@Mock<\/code> annotation tells Mockito that <code>mockList<\/code> is to be treated as a mock and the <code>@RunWith(MockitoJUnitRunner.class)<\/code> tells Mockito to go through all the <code>@Mock<\/code> annotated members of <code>MyTest<\/code> and initialize them for Mocking. You don\u2019t have to assign any new instance to mockList, this is done under the hood for you by Mockito. With this simple code above mockList is ready to be used as a mock.<\/p>\n<p>Try adding the following imports:<\/p>\n<pre class=\"brush:java\">\nimport static org.junit.Assert.*;\nimport static org.mockito.Mockito.*;\nimport org.junit.Test;\n<\/pre>\n<p>And then the following simple test case:<\/p>\n<pre class=\"brush:java\">\n    @Test\n    public void test() {\n   \t String expected = \"Hello, World!\";\n   \t when(mockList.get(0)).thenReturn(expected);\n\n   \t String actual = mockList.get(0);\n   \t assertEquals(expected, actual);\n   \t System.out.println(actual);\n    }\n<\/pre>\n<p>[ulp id=&#8217;JlDLc89DLzIPpdrn&#8217;]<br \/>\n&nbsp;<br \/>\nHere we see that we set up an expectation &#8211; that we have a String <code>\"Hello, World!\"<\/code> and then we go on to stub the <code>List.get()<\/code> method of our mock List to return our expected String when the first element of the list is requested.<\/p>\n<p>We then call <code>mockList.get(0)<\/code> to get our actual value for testing and assert that our actual value is equal to our expected value, and output it to the console for good measure.<\/p>\n<p>At no point have we created a real list, or inserted &#8220;Hello, World!&#8221; into a list. It is just a mock list and the only functionality it has or knows about is the get() method with an input of 0.<\/p>\n<p>Try changing <code>String actual = mockList.get(0);<\/code> to <code>String actual = mockList.get(1);<\/code> and running the test. You will see that <code>actual<\/code> is now null. The reason is that the only functionality we have stubbed is for calling .get() with an input of 0 &#8211; Mockito doesn\u2019t know what to do with an input of 1 so it just returns null. In fact any other method of List we call will return null, and any method which returns nothing will effectively act as a no-op. This is powerful control, in a couple of lines of code we have created an implementation of List that does exactly what we want, when we want it every time it\u2019s called.<\/p>\n<h2><a name=\"best_practices\"><\/a>5. Mockito Best Practices<\/h2>\n<p>Mockito encourages standard best practices in unit testing and design in general, namely:<\/p>\n<ul>\n<li>Mockito does not have a provision for mocking static methods because Mockito encourages object oriented design and dependency injection over procedural code.<\/li>\n<li>Mockito does not have a provision for mocking private methods because public methods should be black boxes and from the standpoint of testing private methods don\u2019t exist.<\/li>\n<li>Mockito packages and encourages the usage of Hamcrest Matchers, which will be covered in subsequent tutorials.<\/li>\n<li>Mockito encourages adherence to the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Law_of_Demeter\">Law of Demeter<\/a> and does not encourage mocking chained methods.<\/li>\n<li>You should not stub or verify on a mock which is shared between different threads. You may call the methods of a shared mock, however.<\/li>\n<li>You can\u2019t verify the <code>toString()<\/code> method of a mock, due to the fact that it may be called by the test environment itself, making verification impossible.<\/li>\n<li>If your test cases use the Given When Then notation you can use the stubbing methods from <code>org.mockito.BDDMockito<\/code> so that <code>when(mock.method()).thenReturn(something)<\/code> becomes <code>given(mock.method()).willReturn(something)<\/code> as it will read nicely in your test format.<\/li>\n<li>It is possible to use Mockito without the Mockito annotations, however it is much easier and neater to use the annotations so that is what we will do in these tutorials.<\/li>\n<li>You can \u2018spy\u2019 on any class, including the class under test if your testing requires that you modify the behaviour of a particular method of the class for the purposes of the test. Mockito explicitly recommends that spies should be only used carefully and occasionally, for instance when constrained by dealing with legacy code. This will be covered in a future tutorial.<\/li>\n<li>In the event that the real call into a spied method could generate an error condition or cannot be called for some other reason Mockito recommends using the do* family of methods for stubbing. This will be covered in a future tutorial.<\/li>\n<li>Mockito will allow you to use argument matchers in place of real arguments with the limitation that if one argument uses a matcher, all must use matchers. Argument matchers will be covered in a later tutorial but should probably be used sparingly.<\/li>\n<li>Mockito provides a <code>verifyNoMoreInteractions()<\/code> method to verify that a particular mock has no more interactions but recommends that it is used very sparingly and only when appropriate.<\/li>\n<li>Mockito provides the <code>Answer<\/code> interface to allow for stubbing with callbacks, however it recommends against using it and encourages you to do simple stubbing using the <code>thenReturn()<\/code> and <code>doThrow()<\/code> methods. We will cover Answers in a later tutorial.<\/li>\n<li>If using <code>ArgumentCaptor<\/code> for argument validation you should use it only in the verification phase, and not the stubbing phase. <code>ArgumentCaptor<\/code> will be covered in a future tutorial.<\/li>\n<li>Mockito recommends to use Partial Mocks very carefully, mainly when dealing with legacy code. Well designed code should not require the use of partial mocks.<\/li>\n<li>Mockito provides a <code>reset()<\/code> method for resetting your mock in the middle of a test method, however it recommends against using it as it is a code smell that your test may be overly long and complex.<\/li>\n<\/ul>\n<p>There are more features and practices, but these are the main ones which Mockito tells you to watch out for. We will cover all of the above and more in depth in the coming tutorials.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Testing with Mockito. In this course, you will dive into the magic of Mockito. You will learn about Mocks, Spies and Partial Mocks, and their corresponding Stubbing behaviour. You will also see the process of Verification with Test Doubles and Object Matchers. Finally, Test Driven Development &hellip;<\/p>\n","protected":false},"author":582,"featured_media":186,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[460,273],"class_list":["post-44886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-mockito","tag-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Getting started with Mockito - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Testing with Mockito. In this course, you will dive into the magic of Mockito. You will learn about\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Getting started with Mockito - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Testing with Mockito. In this course, you will dive into the magic of Mockito. You will learn about\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-15T13:21:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-13T09:06:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/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=\"Hugh Hamill\" \/>\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=\"Hugh Hamill\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html\"},\"author\":{\"name\":\"Hugh Hamill\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/16859fadb54e0a7d918890100afa9c3e\"},\"headline\":\"Getting started with Mockito\",\"datePublished\":\"2015-11-15T13:21:47+00:00\",\"dateModified\":\"2023-12-13T09:06:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html\"},\"wordCount\":2317,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mockito-logo.jpg\",\"keywords\":[\"Mockito\",\"Testing\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html\",\"name\":\"Getting started with Mockito - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mockito-logo.jpg\",\"datePublished\":\"2015-11-15T13:21:47+00:00\",\"dateModified\":\"2023-12-13T09:06:36+00:00\",\"description\":\"This article is part of our Academy Course titled Testing with Mockito. In this course, you will dive into the magic of Mockito. You will learn about\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mockito-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mockito-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/11\\\/getting-started-with-mockito.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Getting started with Mockito\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/16859fadb54e0a7d918890100afa9c3e\",\"name\":\"Hugh Hamill\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4bd03b43df8bac67e2fcb36550d4579d327760fd1b8a141f850e9a100e205df1?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4bd03b43df8bac67e2fcb36550d4579d327760fd1b8a141f850e9a100e205df1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4bd03b43df8bac67e2fcb36550d4579d327760fd1b8a141f850e9a100e205df1?s=96&d=mm&r=g\",\"caption\":\"Hugh Hamill\"},\"description\":\"Hugh is a Senior Software Engineer and Certified Scrum Master based in Galway, Ireland. He achieved his B.Sc. in Applied Computing from Waterford Institute of Technology in 2002 and has been working in industry since then. He has worked for a several large blue chip software companies listed on both the NASDAQ and NYSE.\",\"sameAs\":[\"http:\\\/\\\/www.doubleh.ie\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/hugh-hamill\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Getting started with Mockito - Java Code Geeks","description":"This article is part of our Academy Course titled Testing with Mockito. In this course, you will dive into the magic of Mockito. You will learn about","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:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html","og_locale":"en_US","og_type":"article","og_title":"Getting started with Mockito - Java Code Geeks","og_description":"This article is part of our Academy Course titled Testing with Mockito. In this course, you will dive into the magic of Mockito. You will learn about","og_url":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-11-15T13:21:47+00:00","article_modified_time":"2023-12-13T09:06:36+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mockito-logo.jpg","type":"image\/jpeg"}],"author":"Hugh Hamill","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Hugh Hamill","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html"},"author":{"name":"Hugh Hamill","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/16859fadb54e0a7d918890100afa9c3e"},"headline":"Getting started with Mockito","datePublished":"2015-11-15T13:21:47+00:00","dateModified":"2023-12-13T09:06:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html"},"wordCount":2317,"commentCount":4,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mockito-logo.jpg","keywords":["Mockito","Testing"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html","url":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html","name":"Getting started with Mockito - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mockito-logo.jpg","datePublished":"2015-11-15T13:21:47+00:00","dateModified":"2023-12-13T09:06:36+00:00","description":"This article is part of our Academy Course titled Testing with Mockito. In this course, you will dive into the magic of Mockito. You will learn about","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mockito-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mockito-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/11\/getting-started-with-mockito.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Getting started with Mockito"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/16859fadb54e0a7d918890100afa9c3e","name":"Hugh Hamill","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4bd03b43df8bac67e2fcb36550d4579d327760fd1b8a141f850e9a100e205df1?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4bd03b43df8bac67e2fcb36550d4579d327760fd1b8a141f850e9a100e205df1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4bd03b43df8bac67e2fcb36550d4579d327760fd1b8a141f850e9a100e205df1?s=96&d=mm&r=g","caption":"Hugh Hamill"},"description":"Hugh is a Senior Software Engineer and Certified Scrum Master based in Galway, Ireland. He achieved his B.Sc. in Applied Computing from Waterford Institute of Technology in 2002 and has been working in industry since then. He has worked for a several large blue chip software companies listed on both the NASDAQ and NYSE.","sameAs":["http:\/\/www.doubleh.ie"],"url":"https:\/\/www.javacodegeeks.com\/author\/hugh-hamill"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=44886"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44886\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/186"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=44886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}