{"id":36260,"date":"2016-04-18T11:05:09","date_gmt":"2016-04-18T08:05:09","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=36260"},"modified":"2019-04-09T13:33:29","modified_gmt":"2019-04-09T10:33:29","slug":"spring-test-mock-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/","title":{"rendered":"Spring Test Mock Example"},"content":{"rendered":"<p>A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean &amp; simple API. In this example we will learn how to mock spring components using Mockito. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2<\/p>\n<h2>1. Introduction<\/h2>\n<p>Mockito is a popular mocking framework which can be used in conjunction with JUnit. Mockito allows us to create and configure mock objects. Using Mockito simplifies the development of tests for classes with external dependencies significantly.&nbsp;We can create the mock objects manually or can use the mocking framewors like Mockito, EasyMock. jMock etc.&nbsp;Mock frameworks allow us to create mock objects at runtime and define their behavior.&nbsp;The classical example for a mock object is a data provider. In production a real database is used, but for testing a mock object simulates the database and ensures that the test conditions are always the same.<\/p>\n<p>The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications &#8211; on any kind of deployment platform.<\/p>\n<p>Be able to unit test spring components without the need of loading the full spring-context is a very useful behavior provided by Mockito.\n<\/p>\n<h2>2. Creating a project<\/h2>\n<p>Below are the steps we need to take to create the project.<\/p>\n<ol>\n<li>Open Eclipse. Go to File=&gt;New=&gt;Java Project. In the \u2018Project name\u2019 enter \u2018SpringTestMock\u2019.<\/li>\n<li>Eclipse will create a \u2018src\u2019 folder. Right click on the \u2018src\u2019 folder and choose New=&gt;Package. In the \u2018Name\u2019 text-box enter \u2018com.javacodegeeks\u2019. Click \u2018Finish\u2019.<\/li>\n<\/ol>\n<h3>2.1 Dependencies<\/h3>\n<p>For this example we need the below mentioned jars:<\/p>\n<ul>\n<li>junit-4.1.2<\/li>\n<li>mockito-all-1.10.19<\/li>\n<li>spring-beans-4.2.5.RELEASE<\/li>\n<li>spring-context-4.2.5.RELEASE<\/li>\n<\/ul>\n<p>These jars can be downloaded from <a href=\"http:\/\/search.maven.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Maven repository<\/a>. These are the latest (non-beta) versions available as per now. To add these jars in the classpath right click on the project and choose Build Path=&gt;Configure Build Path. The click on the \u2018Add External JARs\u2019 button on the right hand side. Then go to the location where you have downloaded these jars. Then click ok.<\/p>\n<p><figure id=\"attachment_36261\" aria-describedby=\"caption-attachment-36261\" style=\"width: 718px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/Dependencies.jpg\" rel=\"attachment wp-att-36261\"><img decoding=\"async\" class=\"size-full wp-image-36261\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/Dependencies.jpg\" alt=\"Figure 1. Dependencies\" width=\"718\" height=\"642\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/Dependencies.jpg 718w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/Dependencies-300x268.jpg 300w\" sizes=\"(max-width: 718px) 100vw, 718px\" \/><\/a><figcaption id=\"caption-attachment-36261\" class=\"wp-caption-text\">Figure 1. Dependencies<\/figcaption><\/figure><\/p>\n<h2>3. Code<\/h2>\n<p>To show how to use Mockito for mocking the Spring components we will use the User maintenance example. We will create a service class (UserMaintenanceService) with one method. This class will call the corresponding Data Access Object (DAO) to serve the request. First we will create a simple POJO class which represents the User domain entity.<\/p>\n<p><em><span style=\"text-decoration: underline;\">User.java<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\n\nimport java.util.Date;\n\n\/**\n* Class representing the user domain.\n* @author Meraj\n*\/\npublic class User {\n\n  private Long userId;\n  private String firstName;\n  private String surname;\n  private Date dateOfBirth;\n\n  public Long getUserId() {\n    return userId;\n  }\n\n  public void setUserId(Long userId) {\n    this.userId = userId;\n  }\n\n  public String getFirstName() {\n    return firstName;\n  }\n\n  public void setFirstName(String firstName) {\n    this.firstName = firstName;\n  }\n\n  public String getSurname() {\n    return surname;\n  }\n\n  public void setSurname(String surname) {\n    this.surname = surname;\n  }\n\n  public Date getDateOfBirth() {\n    return dateOfBirth;\n  }\n\n  public void setDateOfBirth(Date dateOfBirth) {\n    this.dateOfBirth = dateOfBirth;\n  }\n}<\/pre>\n<p>Now we will see how the DAO class looks like. The DAO class will be responsible for talking to the database. We will skip that part for this example. This class will be annotated as <code>@Component<\/code>.&nbsp;Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><em><span style=\"text-decoration: underline;\">UserDao.java<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\n\nimport org.springframework.stereotype.Component;\n\n\/**\n* DAO class for User related actions.\n* @author Meraj\n*\/\n@Component\npublic class UserDao {\n\n  \/**\n  * Search for user using the id.\n  * @param id user id\n  * @return Retrieved user\n  *\/\n  public User findUserById(Long id) {\n    \/\/ Find user details from database\n    return new User();\n  }\n}<\/pre>\n<p>Now we will see how the service class looks like. This class will also be annotated with <code>@Component<\/code>. It has the reference to the UserDao class which it injects using the <code>@Autowired<\/code> annotation.<\/p>\n<p>Autowire marks a constructor, field, setter method or config method as to be autowired by Spring&#8217;s dependency injection facilities.&nbsp;Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.&nbsp;Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.&nbsp;Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.<\/p>\n<p><em><span style=\"text-decoration: underline;\">UserMaintenanceService.java<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\n\n\/**\n* Service class for User related actions.\n* @author Meraj\n*\/\n@Component\npublic class UserMaintenanceService {\n\n  @Autowired private UserDao userDao;\n\n  \/**\n  * Find user.\n  * @param userId user id\n  * @return Retrieved user\n  *\/\n  public User findUserById(Long userId) {\n  \/\/ Do business validations.\n    return userDao.findUserById(userId);\n  }\n}<\/pre>\n<h2>4. Test<\/h2>\n<p>Below is the test class which we will use to test in this example.<\/p>\n<p><em><span style=\"text-decoration: underline;\">UserMaintenanceServiceTest.java<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\n\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertNotNull;\nimport static org.junit.Assert.fail;\nimport static org.mockito.Mockito.when;\nimport static org.mockito.MockitoAnnotations.initMocks;\n\nimport java.util.Date;\n\nimport org.junit.Test;\nimport org.mockito.InjectMocks;\nimport org.mockito.Mock;\n\npublic class UserMaintenanceServiceTest {\n\n  @InjectMocks private UserMaintenanceService userMaintenanceService;\n  @Mock private UserDao userDao;\n\n  @Test\n  public void testFindUserByIdPositive() {\n    initMocks(this);\n    when(userDao.findUserById(1000L)).thenReturn(getMeTestUser());\n    User user = userMaintenanceService.findUserById(1000L);\n    assertNotNull(user);\n    assertEquals(\"Test first name\", user.getFirstName());\n    assertEquals(\"Test surname\", user.getSurname());\n  }\n\n  @Test (expected = NullPointerException.class)\n  public void testFindUserByIdNegetive() {\n    userMaintenanceService = new UserMaintenanceService();\n    userMaintenanceService.findUserById(1000L);\n    fail();\n}\n\n  private User getMeTestUser() {\n    User user = new User();\n    user.setUserId(1000L);\n    user.setFirstName(\"Test first name\");\n    user.setSurname(\"Test surname\");\n    user.setDateOfBirth(new Date());\n    return user;\n  }\n}\n<\/pre>\n<p>Now we will discuss few things in this class. If you would have notice you will see that the&nbsp;UserMaintenanceService class is annotated with&nbsp;<code>@InjectMocks<\/code>. This marks a field on which injection should be performed. It minimizes repetitive mock and spy injection.&nbsp;Mockito will try to inject mocks only either by constructor injection, setter injection, or property injection in order and as described below. If any of the following strategy fail, then Mockito won&#8217;t report failure; i.e. you will have to provide dependencies yourself.[ulp id=&#8217;GhnY46zldrhgoeym&#8217;]<\/p>\n<ol>\n<li><strong>Constructor injection:&nbsp;<\/strong>the biggest constructor is chosen, then arguments are resolved with mocks declared in the test only.&nbsp;<strong><em>Note<\/em>:&nbsp;<\/strong>If arguments can not be found, then null is passed. If non-mockable types are wanted, then constructor injection won&#8217;t happen. In these cases, you will have to satisfy dependencies yourself.<\/li>\n<li><strong>Property setter injection:&nbsp;<\/strong>mocks will first be resolved by type, then, if there is several property of the same type, by the match of the property name and the mock name.&nbsp;<strong><em>Note<\/em>:&nbsp;<\/strong>If you have properties with the same type (or same erasure), it&#8217;s better to name all <code>@Mock<\/code> annotated fields with the matching properties, otherwise Mockito might get confused and injection won&#8217;t happen.&nbsp;If <code>@InjectMocks<\/code> instance wasn&#8217;t initialized before and have a no-arg constructor, then it will be initialized with this constructor.<\/li>\n<li><strong>Field injection<\/strong>&nbsp;mocks will first be resolved by type, then, if there is several property of the same type, by the match of the field name and the mock name.&nbsp;<strong><em>Note<\/em>:&nbsp;<\/strong>If you have fields with the same type (or same erasure), it&#8217;s better to name all <code>@Mock<\/code> annotated fields with the matching fields, otherwise Mockito might get confused and injection won&#8217;t happen.&nbsp;If <code>@InjectMocks<\/code> instance wasn&#8217;t initialized before and have a no-arg constructor, then it will be initialized with this constructor.<\/li>\n<\/ol>\n<p>The UserDao class is annotated with <code>@Mock<\/code>. This is the class which we want to mock.<\/p>\n<p>In the first test method the first thing we do is call the&nbsp;<code>MockitoAnnotations.initMocks()<\/code> method. It initializes objects annotated with <code>@Mock<\/code> for given test class. Then we define the behaviour of the DAO class method by using the&nbsp;<code>org.mockito.Mockito.when()<\/code>. We return our own test User object here.<\/p>\n<p>In the second test we are not calling the&nbsp;<code>MockitoAnnotations.initMocks()<\/code> so the DAO class will not be injected in this case hence it will throw <code>NullPointerException<\/code>.<\/p>\n<h2>5. Download the source file<\/h2>\n<p>This was an example of <span style=\"font-family: monospace;\">mocking spring components<\/span>.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here:&nbsp;<strong><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/SpringTestMock.zip\" rel=\"\">SpringTestMock<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean &amp; simple API. In this example we will learn how to mock spring components using Mockito. Tools and technologies used in this example are &hellip;<\/p>\n","protected":false},"author":34,"featured_media":21338,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[923],"tags":[],"class_list":["post-36260","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>Spring Test Mock Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write\" \/>\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\/spring-test-mock-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Test Mock Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-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=\"2016-04-18T08:05:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-09T10:33:29+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=\"Mohammad Meraj Zia\" \/>\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=\"Mohammad Meraj Zia\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 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\/spring-test-mock-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/\"},\"author\":{\"name\":\"Mohammad Meraj Zia\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed\"},\"headline\":\"Spring Test Mock Example\",\"datePublished\":\"2016-04-18T08:05:09+00:00\",\"dateModified\":\"2019-04-09T10:33:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/\"},\"wordCount\":1091,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-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\/spring-test-mock-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/\",\"name\":\"Spring Test Mock Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"datePublished\":\"2016-04-18T08:05:09+00:00\",\"dateModified\":\"2019-04-09T10:33:29+00:00\",\"description\":\"A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-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\/spring-test-mock-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\":\"Spring Test Mock 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\/442b4f9b8a4aa7e12376464fc354f8ed\",\"name\":\"Mohammad Meraj Zia\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/06\/IMG-20200324-WA0003-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/06\/IMG-20200324-WA0003-96x96.jpg\",\"caption\":\"Mohammad Meraj Zia\"},\"description\":\"Senior Java Developer\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/mohammad-zia\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Test Mock Example - Java Code Geeks","description":"A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write","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\/spring-test-mock-example\/","og_locale":"en_US","og_type":"article","og_title":"Spring Test Mock Example - Java Code Geeks","og_description":"A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-04-18T08:05:09+00:00","article_modified_time":"2019-04-09T10:33:29+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":"Mohammad Meraj Zia","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Mohammad Meraj Zia","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/"},"author":{"name":"Mohammad Meraj Zia","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed"},"headline":"Spring Test Mock Example","datePublished":"2016-04-18T08:05:09+00:00","dateModified":"2019-04-09T10:33:29+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/"},"wordCount":1091,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-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\/spring-test-mock-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/","name":"Spring Test Mock Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","datePublished":"2016-04-18T08:05:09+00:00","dateModified":"2019-04-09T10:33:29+00:00","description":"A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/spring-test-mock-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\/spring-test-mock-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":"Spring Test Mock 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\/442b4f9b8a4aa7e12376464fc354f8ed","name":"Mohammad Meraj Zia","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/06\/IMG-20200324-WA0003-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/06\/IMG-20200324-WA0003-96x96.jpg","caption":"Mohammad Meraj Zia"},"description":"Senior Java Developer","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/mohammad-zia\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36260","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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=36260"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36260\/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=36260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=36260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=36260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}