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

  • Home
  • SEARCH
  • 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 7980757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:10:23+00:00 2026-06-04T10:10:23+00:00

I am using Mockito to write tests for code. However I am stuck at

  • 0

I am using Mockito to write tests for code. However I am stuck at following scenario –
Class A has 2 methods, method1() and method2(). I tried using ArgumentCaptor to catch values sent to method2(). But, since I am using @Spy, I cannot use Matchers.

How do I test method1()?

class A{
    B b;
    method1(arg1, arg2){
       //some logic
       method2(arg1, arg2, ....argN);
    }

   method2(arg1, arg2,....argN){
       //some logic
       b.method3(arg1, arg2...);
   }
}

How to verify method2 receives same argument values?
Following is the test class I wrote:

Class TestA{

@Mock
B b;

@Spy
@InjectMocks   //required else b is null
A a = new A();

@Test
public void testMethod1(){

 a.method1(arg1, arg2);

  //How to verify method2 receives same argument values (arg1, arg2)????
  //verify(a, times(1)).method2(.......);   
}

}

  • 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-04T10:10:24+00:00Added an answer on June 4, 2026 at 10:10 am

    I was intrigued by this post and the comments left by @David so I decided to code a working example for those who follow like me

    /*
     * class to test
     */
    public class A {
    
        public void methodA(String str) {
            this.methodB(str);
        }
    
        protected void methodB(String str) {
            // some implementation
        }
    }
    

    We want to assert that the value being passed into methodB() is what we expect. Reading up on the ArgumentCaptor led me to discover the equivalent Captor Annotation

    import static org.junit.Assert.assertEquals;
    import static org.mockito.Mockito.verify;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.ArgumentCaptor;
    import org.mockito.Captor;
    import org.mockito.Spy;
    import org.mockito.runners.MockitoJUnitRunner;
    
    @RunWith(MockitoJUnitRunner.class)
    public class MultipleMethodCallTest {
    
        @Spy
        A a = new A();
    
        @Captor ArgumentCaptor<String> captor;
    
        @Test
        public void captureSecondMethodCallArgument() throws Exception {
    
            // EXPECTED
    
            String greeting = "hello world";
    
            // PERFORM TEST
    
            a.methodA(greeting);
    
            // ASSERT
    
            verify(a).methodB(captor.capture());
    
            assertEquals(greeting, captor.getValue());
        }
    }
    

    This example was tested with

    • mockito-all-1.8.5.jar
    • junit-4.8.2.jar
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Mockito in some tests. I have the following classes: class BaseService {
Using LINQ on collections, what is the difference between the following lines of code?
I am trying to mock a class using Mockito and also using PowerMock. This
I just started using mock objects (using Java's mockito) in my tests recently. Needless
I'm trying to test some legacy code, using Mockito. I want to stub a
I'm using Mockito for my unit tests and I encounetered a problem with throwing
I'm using JUnit with Mockito. PowerMock can mock static methods but it doesn't seem
I'm trying to create a test using Mockito, however I'm getting a NullPointerException. The
I'm trying to generate code coverage reports with EMMA using tests of which some
I'm using Mockito as a part of Specs in scala code and I've stumbled

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.