0

service.searchRaashanRegister contains another method adresseDao.findRaashan(crit) for which should return particular result for parameter setUnit "BUBBA"

  SearchResult<Raashan> searchResult = new SearchResult<Raashan>();
  searchResult.setSize(1);
  searchResult.setResult(Arrays.asList(raashan));

  SearchCriteria crit = new SearchCriteria ();
  crit.setUnit(new HashSet<String>(Arrays.asList("BUBBA")));
  crit.setPage(0);
  crit.setPageSize(15);
  crit.setSort(null);
  crit.setQuery("");
  crit.setSortAsc(Boolean.TRUE);

  when(raashanDao.findRaashan(crit)).thenReturn(searchResult);

  //This request is sent to web service from which above criteria is created.
  RaashanSearchRequest searchRequest = new RaashanSearchRequest ();
  searchRequest .setPage(0);
  searchRequest .setPageSize(15);
  searchRequest .setUnit("BUBBA");
  searchRequest .setQuery("");
  searchRequest .setSort(null);
  searchRequest .setSortASC(Boolean.TRUE);

  RaashanResponse response = service.searchRaashanRegister(searchRequest );

The above code is not giving desired results and adresseDao.findRaashan(crit) is returning Null. When I use any(SearchCriteria.class) then test pass okay but we want results on particular critera.

I also added initMocks in @Before:

@Before
public void init() {
  MockitoAnnotations.initMocks(this);

Unable to get what is reason where error occurs.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
fatherazrael
  • 5,511
  • 16
  • 71
  • 155
  • Please read [mcve]. You want to have all **relevant** code in your question, instead of giving further bits as comments on answers. The question needs to stand on its own, having all the things in it we need to answer it. This clearly doesn't – GhostCat Jun 08 '17 at 09:12

1 Answers1

0

As far as I understand your code, I think you need to use eq matcher -org.mockito.Matchers.eq if planning to set expectations on specific instances.

I am assuming raashanDao is a mocked instance , so it would be like ,

when(raashanDao.findRaashan(eq(crit))).thenReturn(searchResult);

UPDATE : For previous discussion, I think, I misunderstood your question. eq(crit) should work as is in when as well as in verify without setting anything since that is a method parameter.

A sample is shown here

You use any for parameter values that you don't care and use eq for specific values.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • eq(crit) does not work. I do not understand (need to set same crit instance to your raashanDao mocked instance too). In @Before we already have service class initialized having above mocked object as parameter – fatherazrael Jun 08 '17 at 06:15
  • Code is like @Before public void init() { MockitoAnnotations.initMocks(this); service = new RaashanServicesImpl(RaashanDao, manlingDao, quatroDao); } – fatherazrael Jun 08 '17 at 06:20
  • crit is criteria argument inside findRaashan method -> raashanDao.findRaashan(crit). It is an object having various arguments as shown in above snippet – fatherazrael Jun 08 '17 at 06:22
  • That is why your `any` matcher works since you are not bothered for specific instances. You will not be able to use `eq` unless you change your code to have some way to set that `crit` . All in all, value used `eq` should be available when actual method call happens. There are many ways to achieve that. – Sabir Khan Jun 08 '17 at 06:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146131/discussion-between-fatherazrael-and-sabir-khan). – fatherazrael Jun 08 '17 at 06:25