JUnit Testing. Type mismatch: cannot convert from Store to List<Store>

Viewed 39

I think I miss the syntax at "thenReturn" part. What should I wrote for the source code?

enter image description here

here's the source code:

@Test
public void testGetUserByEmail() {
    String email = "mail";
    when(storeService.search(Mockito.anyString())).thenReturn(Mockito.any(Store.class));
    ModelAndView mav = storeController.search(email);
    Assert.assertEquals("userDetail", mav.getViewName());
}

here's the service:

public List<Store> listAllStore(){
    logger.info("Before add repository to show all");
    List<Store> all_stores=(List<Store>) storeRepo.findAll();
    logger.info("After add repository to show all");
    logger.info(all_stores);
    return all_stores;
}
1 Answers

Few observations

  1. when(storeService.search(Mockito.anyString())).thenReturn(Mockito.any(Store.class)); theReturn should provide some actual data when storeService.search() is called. It should be something like when(storeService.search(Mockito.anyString())).thenReturn(testData); where testData is of type storeService.search() returns.
  2. In your test case, you are mocking storeService.search() but in your example, you have provided definition of listAllStore. Which one is correct? Either you need to fix your question or test case.
Related