{"id":35128,"date":"2016-03-25T18:00:10","date_gmt":"2016-03-25T16:00:10","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=35128"},"modified":"2019-04-09T13:34:17","modified_gmt":"2019-04-09T10:34:17","slug":"mockito-captor-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/","title":{"rendered":"Mockito Captor 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 use ArgumentCaptor class\/ Captor annotation of 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.\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 \u2018MockitoCaptorExample\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<li>Right click on the package and choose New=&gt;Class. Give the class name as MockitoCaptorExample. Click \u2018Finish\u2019. Eclipse will create a default class with the given name.<\/li>\n<\/ol>\n<h3>2.1 Dependencies<\/h3>\n<p>For this example we need the junit and mockito jars. These jars can be downloaded from <a href=\"http:\/\/search.maven.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Maven repository<\/a>. We are using \u2018junit-4.12.jar\u2019 and \u2018mockito-all-1.10.19.jar\u2019. There are the latests (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<h2>3. ArgumentCaptor class<\/h2>\n<p><code>ArgumentCaptor<\/code> class is used&nbsp;to capture argument values for further assertions.&nbsp;Mockito verifies argument values in natural java style: by using an <code>equals()<\/code> method.&nbsp;This is also the recommended way of matching arguments because it makes tests clean &amp; simple. In some situations though, it is helpful to assert on certain arguments after the actual verification.&nbsp;For example:<\/p>\n<pre class=\"brush:java\">ArgumentCaptor&lt;Contact&gt; argument = ArgumentCaptor.forClass(Contact.class);\nverify(mockClass).doSomething(argument.capture());\nassertEquals(\"Meraj\", argument.getValue().getName());\n<\/pre>\n<p>It is recommended to use <code>ArgumentCaptor<\/code> with verification but not with stubbing. Using <code>ArgumentCaptor<\/code> with stubbing may decrease test readability because captor is created outside of assert (aka verify or &#8216;then&#8217;) block. Also it may reduce defect localization because if stubbed method was not called then no argument is captured.<\/p>\n<p>In a way <code>ArgumentCaptor<\/code> is related to custom argument matchers.&nbsp;Both techniques can be used for making sure certain arguments where passed to mocks. However, <code>ArgumentCaptor<\/code> may be a better fit if:<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>custom argument matcher is not likely to be reused<\/li>\n<li>you just need it to assert on argument values to complete verification<\/li>\n<\/ul>\n<p>Custom argument matchers via <code>ArgumentMatcher<\/code> are usually better for stubbing.<\/p>\n<h3>3.1 Methods<\/h3>\n<p>In this section we will describe the methods defined in the <code>ArgumentCaptor<\/code> class.<\/p>\n<h4>3.1.1 public T capture()<\/h4>\n<p>Use it to capture the argument. This method must be used inside of verification.&nbsp;Internally, this method registers a special implementation of an <code>ArgumentMatcher<\/code>. This argument matcher stores the argument value so that you can use it later to perform assertions.<\/p>\n<h4>3.1.2&nbsp;public T getValue()<\/h4>\n<p>Returns the captured value of the argument.&nbsp;If the method was called multiple times then it returns the latest captured value.<\/p>\n<h4>3.1.3&nbsp;public java.util.List&lt;T&gt; getAllValues()<\/h4>\n<p>Returns all captured values. Use it in case the verified method was called multiple times.<\/p>\n<h2>4. Captor annotation<\/h2>\n<p>Captor annotation allows shorthand <code>ArgumentCaptor<\/code> creation on fields. One of the advantages of using <code>@Captor<\/code> annotation is that you can avoid warnings related capturing complex generic types. The Captor annotation is defined as below:<\/p>\n<pre class=\"brush:java\">@Retention(value=RUNTIME)\n@Target(value=FIELD)\n@Documented\npublic @interface Captor\n<\/pre>\n<h2>5. Code<\/h2>\n<p>In this section first we wee see a simple example of using the @Captor annotation. Then we will discuss a more complex one.<\/p>\n<h3>5.1 Simple Code<\/h3>\n<p>For this simple example we will make use of the&nbsp;java.util.Stack class. We will create a stack of strings then add one value to it. Then we will capture he argument and verify it. Below is the code snippet for this:<\/p>\n<pre class=\"brush:java\">stack.add(\"Java Code Geeks\");\nMockito.verify(stack).add(argumentCaptor.capture());\nassertEquals(\"Java Code Geeks\", argumentCaptor.getValue());\n<\/pre>\n<p>In the second example we will add two values in the Stack and will extract all the values using the&nbsp;getAllValues() method. Below is the code snippet for this:<\/p>\n<pre class=\"brush:java\">stack.add(\"Java Code Geeks\");\nstack.add(\"Mockito\");\nMockito.verify(stack, Mockito.times(2)).add(argumentCaptor.capture());\nList&lt;String&gt; values = argumentCaptor.getAllValues();\nassertEquals(\"Java Code Geeks\", values.get(0));\nassertEquals(\"Mockito\", values.get(1));\n<\/pre>\n<p>Below is the code which shows the usage of <code>@Captor<\/code> annotation<\/p>\n<p><span style=\"text-decoration: underline\"><em>MockitoCaptorExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\n\nimport static org.junit.Assert.assertEquals;\n\nimport java.util.Stack;\n\nimport org.junit.Before;\nimport org.junit.Test;\nimport org.mockito.ArgumentCaptor;\nimport org.mockito.Captor;\nimport org.mockito.Mock;\nimport org.mockito.Mockito;\nimport org.mockito.MockitoAnnotations;\n\npublic class MockitoCaptorExample {\n\n  @Mock Stack&lt;String&gt; stack;\n  @Captor ArgumentCaptor&lt;String&gt; argumentCaptor;\n\n  @Before\n  public void setUp() {\n    MockitoAnnotations.initMocks(this);\n  }\n\n  @Test\n  public void test() throws Exception {\n    stack.add(\"Java Code Geeks\");\n    Mockito.verify(stack).add(argumentCaptor.capture());\n    assertEquals(\"Java Code Geeks\", argumentCaptor.getValue());\n  }\n}\n<\/pre>\n<h3>5.2 Stub example<\/h3>\n<p>In this section we will see how we can use @Captor for stubbing. We will use the report generation example.[ulp id=&#8217;GhnY46zldrhgoeym&#8217;]<\/p>\n<p>Create an interface with one method.<\/p>\n<p><em><span style=\"text-decoration: underline\">IReportGenerator.java<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\n\n\/**\n* Interface for generating reports.\n* @author Meraj\n*\/\npublic interface IReportGenerator {\n\n  \/**\n  * Generate report.\n  * @param report Report entity.\n  *\/\n  void generateReport(ReportEntity report);\n}<\/pre>\n<p>Now we will create the report entity class which is a simple POJO class.<\/p>\n<p><em><span style=\"text-decoration: underline\">ReportEntity.java<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\n\nimport java.util.Date;\n\n\/**\n* Report entity.\n* @author Meraj\n*\/\npublic class ReportEntity {\n\n  private Long reportId;\n  private Date startDate;\n  private Date endDate;\n  private byte[] content;\n\n  public Long getReportId() {\n    return reportId;\n  }\n\n  public void setReportId(Long reportId) {\n    this.reportId = reportId;\n  }\n\n  public Date getStartDate() {\n    return startDate;\n  }\n\n  public void setStartDate(Date startDate) {\n    this.startDate = startDate;\n  }\n\n  public Date getEndDate() {\n    return endDate;\n  }\n\n  public void setEndDate(Date endDate) {\n    this.endDate = endDate;\n  }\n\n  public byte[] getContent() {\n    return content;\n  }\n\n  public void setContent(byte[] content) {\n    this.content = content;\n  }\n}<\/pre>\n<p>Now we will have a look at the service class which we will use to generate the report.<\/p>\n<p><em><span style=\"text-decoration: underline\">ReportGeneratorService.java<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\n\nimport java.util.Date;\n\n\/**\n* Service class for generating report.\n* @author Meraj\n*\/\npublic class ReportGeneratorService {\n\n  private IReportGenerator reportGenerator;\n\n  \/**\n  * Generate report.\n  * @param startDate start date\n  * @param endDate end date\n  * @param content report content\n  *\/\n  public void generateReport(Date startDate, Date endDate, byte[] content) {\n    ReportEntity report = new ReportEntity();\n    report.setContent(content);\n    report.setStartDate(startDate);\n    report.setEndDate(endDate);\n    reportGenerator.generateReport(report);\n  }\n}\n<\/pre>\n<p>Now we will look at the test.<\/p>\n<p><em><span style=\"text-decoration: underline\">ReportGeneratorServiceTest.java<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\n\nimport static org.junit.Assert.assertEquals;\n\nimport java.util.Calendar;\n\nimport org.junit.Before;\nimport org.junit.Test;\nimport org.mockito.ArgumentCaptor;\nimport org.mockito.Captor;\nimport org.mockito.InjectMocks;\nimport org.mockito.Mock;\nimport org.mockito.Mockito;\nimport org.mockito.MockitoAnnotations;\n\npublic class ReportGeneratorServiceTest {\n\n  @InjectMocks private ReportGeneratorService reportGeneratorService;\n  @Mock private IReportGenerator reportGenerator;\n  @Captor private ArgumentCaptor&lt;ReportEntity&gt; reportCaptor;\n\n  @Before\n  public void setUp() {\n    MockitoAnnotations.initMocks(this);\n  }\n\n  @SuppressWarnings(\"deprecation\")\n  @Test\n  public void test() {\n    Calendar startDate = Calendar.getInstance();\n    startDate.set(2016, 11, 25);\n    Calendar endDate = Calendar.getInstance();\n    endDate.set(9999, 12, 31);\n    String reportContent = \"Report Content\";\n    reportGeneratorService.generateReport(startDate.getTime(), endDate.getTime(), reportContent.getBytes());\n\n    Mockito.verify(reportGenerator).generateReport(reportCaptor.capture());\n\n    ReportEntity report = reportCaptor.getValue();\n\n    assertEquals(116, report.getStartDate().getYear());\n    assertEquals(11, report.getStartDate().getMonth());\n    assertEquals(25, report.getStartDate().getDate());\n\n    assertEquals(8100, report.getEndDate().getYear());\n    assertEquals(0, report.getEndDate().getMonth());\n    assertEquals(31, report.getEndDate().getDate());\n\n    assertEquals(\"Report Content\", new String(report.getContent()));\n  }\n}<\/pre>\n<h2>6. Download the source file<\/h2>\n<p>This was an example of <code>Mockito Captor annotation<\/code>.<\/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\/03\/MockitoCaptorxample.zip\" rel=\"\"><strong>Mockito Captor Example<\/strong><\/a><\/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 use ArgumentCaptor class\/ Captor annotation of Mockito. Tools and technologies used in this &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-35128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mockito"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Mockito Captor 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\/mockito-captor-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mockito Captor 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\/mockito-captor-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-03-25T16:00:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-09T10:34:17+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\/mockito-captor-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/\"},\"author\":{\"name\":\"Mohammad Meraj Zia\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed\"},\"headline\":\"Mockito Captor Example\",\"datePublished\":\"2016-03-25T16:00:10+00:00\",\"dateModified\":\"2019-04-09T10:34:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/\"},\"wordCount\":868,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"articleSection\":[\"Mockito\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/\",\"name\":\"Mockito Captor Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"datePublished\":\"2016-03-25T16:00:10+00:00\",\"dateModified\":\"2019-04-09T10:34:17+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\/mockito-captor-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Mockito\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/mockito\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Mockito Captor 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":"Mockito Captor 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\/mockito-captor-example\/","og_locale":"en_US","og_type":"article","og_title":"Mockito Captor 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\/mockito-captor-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-03-25T16:00:10+00:00","article_modified_time":"2019-04-09T10:34:17+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\/mockito-captor-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/"},"author":{"name":"Mohammad Meraj Zia","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed"},"headline":"Mockito Captor Example","datePublished":"2016-03-25T16:00:10+00:00","dateModified":"2019-04-09T10:34:17+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/"},"wordCount":868,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","articleSection":["Mockito"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/","name":"Mockito Captor Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","datePublished":"2016-03-25T16:00:10+00:00","dateModified":"2019-04-09T10:34:17+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\/mockito-captor-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/mockito-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/mockito\/mockito-captor-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"Mockito","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/mockito\/"},{"@type":"ListItem","position":5,"name":"Mockito Captor 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\/35128","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=35128"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/35128\/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=35128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=35128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=35128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}