JUnit测试,类型不匹配:无法从存储转换为列表< Store>

wf82jlnq  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(87)

我想我错过了“thenReturn”部分的语法。我应该为源代码写些什么呢?

下面是源代码:

@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());
}

服务是这样的:

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;
}
l7mqbcuq

l7mqbcuq1#

观察结果很少

  1. when(storeService.search(Mockito.anyString())).thenReturn(Mockito.any(Store.class)); theReturn应该在调用www.example.com()时提供一些实际数据storeService.search。它应该类似于when(storeService.search(Mockito.anyString())).thenReturn(testData);,其中testData的类型为storeService.search()返回的类型。
    1.在您的测试用例中,您模拟了storeService.search(),但在您的示例中,您提供了listAllStore的定义。哪一个是正确的?您需要修复您的问题或测试用例。

相关问题