0

I’m using Mockito 1.9.5, Maven 3.2.3, and JUnit 4.11. I have the following set up in a JUnit test ...

    final Set<Account> accounts = new HashSet<Account>();
    final Account acct1 = new Account();
    acct1.setId(“id1”);
    accounts.add(acct1);
    …
    Mockito.doReturn(acctTeamMemberMap).when(m_accountTeamMemberDao).getManagers(accounts);

The problem is the Account objects I’m creating in my JUnit test are not the same objects that are actually being passed in as parameters when I execute the method call. My question is, how do I tell Mockito to return the results when the ID fields of the various objects match the IDs I have in my JUnit test? In other words, if the above Account objects have ids “id1” and “id2”, I would want Mockito to return the results always if the actual parameters also have ids “id1” and “id2”.

Thanks, - Dave

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

1

You can use any() matcher from Mockito, and just return result no matter what value was passed:

Mockito.doReturn(acctTeamMemberMap).when(any()).getManagers(accounts);

Alternatively you can implement your own matcher and check if value that is was passed follows the specified constraints:

class IsListOfTwoElements extends ArgumentMatcher<List> {
      public boolean matches(Object myDao) {
          String id =  ((MyDao) myDao).size().getId();
          return id.equals("id1") || id.equals("id2");
      }
   }
Community
  • 1
  • 1
Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67