Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:26:20+00:00 2026-06-10T23:26:20+00:00

In Mockito documentation and javadocs it says It is recommended to use ArgumentCaptor with

  • 0

In Mockito documentation and javadocs it says

It is recommended to use ArgumentCaptor with verification but not with stubbing.

but I don’t understand how ArgumentCaptor can be used for stubbing. Can someone explain the above statement and show how ArgumentCaptor can be used for stubbing or provide a link that shows how it can be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:26:21+00:00Added an answer on June 10, 2026 at 11:26 pm

    Assuming the following method to test:

    public boolean doSomething(SomeClass arg);
    

    Mockito documentation says that you should not use captor in this way:

    when(someObject.doSomething(argumentCaptor.capture())).thenReturn(true);
    assertThat(argumentCaptor.getValue(), equalTo(expected));
    

    Because you can just use matcher during stubbing:

    when(someObject.doSomething(eq(expected))).thenReturn(true);
    

    But verification is a different story. If your test needs to ensure that this method was called with a specific argument, use ArgumentCaptor and this is the case for which it is designed:

    ArgumentCaptor<SomeClass> argumentCaptor = ArgumentCaptor.forClass(SomeClass.class);
    verify(someObject).doSomething(argumentCaptor.capture());
    assertThat(argumentCaptor.getValue(), equalTo(expected));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im learning Mockito and in chapter 16 they say you should not use partial
I am new to Mockito. Given the class below, how can I use Mockito
With Mockito I can do the following: verify(someService).process(any(Person.class)); But how do I write this
Can I use Mockito to capture what was passed to the HttpServletResponse#sendError() method? I
I want to use Mockito to test the (simplified) code below. I don't know
I'm using JUnit with Mockito. PowerMock can mock static methods but it doesn't seem
I'm writing a test case where I'm trying to use mockito to avoid entering
Mockito api provides method: Mockito.verifyNoMoreInteractions(someMock); but is it possible in Mockito to declare that
Consider a method signature like: public String myFunction(String abc); Can Mockito help return the
I use mockito as a mock object library. I am unit testing DAOs. DAOs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.