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 7728403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:42:30+00:00 2026-06-01T05:42:30+00:00

I am creating a general general mock-client for testing HTTP-interactions. For this, I would

  • 0

I am creating a general general mock-client for testing HTTP-interactions. For this, I would like to be able to make a number of responses of the same method.
With a normal mock, this would not be a problem:

when(mock.execute(any(), any(), any())).thenReturn(firstResponse, otherResponses)

However, I am using a partial mock, where I simply want to mock method making the HTTP request, since there might not be access to a live end-point or the Internet in general for that matter in the context where the unit-tests are executed.

So I will be doing something like:

doReturn(response).when(spy).execute(hostCaptor.capture(), requestCaptor.capture(), contextCaptor.capture());

However, I would like to be able to support more than one response (not much of an “interaction”). But there are no doReturn-method, which takes a more than a single response at a time.

My first attempt on a solution was to do it iteratively:

Stubber stubber = null;
for (HttpResponse response : responses) {
    if (stubber == null) {
        stubber = doReturn(response);
    } else {
        stubber = stubber.doReturn(response);
    }
}
stubber.when(spy).execute(hostCaptor.capture(), requestCaptor.capture(), contextCaptor.capture());

This does however fail to verify (“Unfinished stubbing detected”) when running the test.

So – is there a way to achieve this with Mockito?

Thanks for reading.

  • 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-01T05:42:31+00:00Added an answer on June 1, 2026 at 5:42 am

    You can write

    doReturn( 1 ).doReturn( 2 ).doReturn( 3 ).when( myMock ).myMethod( any(), any(), any());
    

    Edit:

    If the values you want are in the array myArray, then you could also use

    import static java.util.Arrays.asList;
    import static org.mockito.Mockito.doAnswer;
    import org.mockito.stubbing.answers.ReturnElementsOf
    
    ....
    
    doAnswer( new ReturnsElementsOf( asList( myArray )))
       .when( myMock ).myMethod( any(), any(), any());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would you go about creating something like this http://www.bikingboss.com/ (move your mouse horizontally).
I would like to know if there are general rules for creating an index
I'm new to unit testing using nunit (and Java development in general). When creating
Could somebody show me how you would go about creating a mock HTML Helper
I'm creating a general purpose web service that is likely to have a number
I'm creating an accounting-like software and doing the General Ledger. Now my plan is
What's the best general technique for creating an IMAP client and keeping its local
This is a general best practice question about creating parent / child relationships with
I've been looking at the article on creating static constructors in c# here: http://csharpindepth.com/Articles/General/Singleton.aspx
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=

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.