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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:35:18+00:00 2026-05-28T03:35:18+00:00

I have a method, which accept a string and an iterator: public int doSomething(String

  • 0

I have a method, which accept a string and an iterator:

public int doSomething(String str, Iterator<String> itr)

I am trying to mock a class, where this method is, so it would return me integer, depending on passed arguments. How can I call Mockito’s when() so it expects passed iterator to be “equal” to the one I specify? As the passed iterator is build somewhere dynamically in system, I cannot use the same instance of it in when(), so I can only make a copy of it as my expectation:

List<String> aList = new ArrayList<String>();
aList.add("one");
aList.add("two");

MyClass myMock = Mockito.mock(MyClass.class);

I have tried following, and none of them seems to work:

Mockito.when(myMock.doSomething("some string", aList.iterator())).thenReturn(10);
Mockito.when(myMock.doSomething(Matchers.eq("some string"), Matchers.eq(aList.iterator()))).thenReturn(10);

I only succeeded with using anyObject():

Mockito.when(myMock.doSomething(Matchers.eq("some string"), Matchers.anyObject())).thenReturn(10);

but then of course I cannot set different results, depending on what is in iterator…

  • 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-05-28T03:35:19+00:00Added an answer on May 28, 2026 at 3:35 am

    You could use

    when(myMock.doSomething(eq("some string"), argThat(new IsIteratorOfList(aList))))
    

    where IsIteratorOfList is a subclass of ArgumentMatcher which checks that when the given iterator’selements are all added to a new list, this new list equals the list passed in the constructor (aList in this case).

    The matcher could be even simpler and just check the first element, or the number of elements, or anything you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have webservice with one method which recieves xml as string: [WebMethod] public int
I have a method which accept jagged array of Objects. public void MyDataBind(object[][] data)
I have a method, which will accept a parameter of a JQuery Object and
I have the following C# helper class which retrieves a Json String from a
My application will basically accept user input via the main(String[] args) method. I have
I have a web service with just 1 Web method which will accept xml
I have three resources: @RequestMapping(value = sample, method = RequestMethod.GET, headers = Accept=text/html) public
Aloha I have a method with (pseudo) signature: public static T Parse<T>(string datadictionary) where
Here's the situation. I have a base class which implements a method that accepts
I'm trying to split a string using the method found in this thread ,

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.