我要进行单元测试。但是最终我得到了500错误,因此我设置了断点并以调试模式运行测试。最终,我发现服务方法没有被调用并返回null。
我只想知道为什么它跳过articleService.getInfo
,然后使对象变为null,最后出现500错误。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Controller.class)
@WebAppConfiguration
public class ControllerTest {
private MockMvc mockMvc;
@MockBean
private TestService testService;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void testList() throws Exception{
ObjectMapper mapper = new ObjectMapper();
String jsonStr = Files.readString(Paths.get("src/test/resources/data/list.json"));
BookMarkResponse response = mapper.readValue(jsonStr, BookMarkResponse.class);
Mockito.when(bookmarkService.getDocument(Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response);
MvcResult result = mockMvc.perform(get("/getData")
.param("Id",Id))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
}
@RequestMapping(method = RequestMethod.GET, value="/getData")
@ResponseBody
public RandomList getList(@RequestParam("Id") int Id) {
Response bookmarkTest = new Response();
cTest = bookmarkService.getDocument(string, string, string, string, string, string, string);
if(cTest.getResponseStatus() != 1){
bkmark.setCode("Error");
return bkmark;
}
List<NBookMark> nbookMarks = new ArrayList<NBookMark>();
if( cTest.getTotal() > 0){
nbookMarks = convert2List(cTest);
bkmark.setCode("OK");
}
bkmark.setBookmarks(nbookMarks);
return bkmark;
}
public List<NBookMark> convert2List(BookMarkResponse input) {
List<NBookMark> Bookmarks = new ArrayList<NBookMark>();
List<BookMarkDocument> bookmarks = input.getBookmarks();
for(BookMarkDocument item: bookmarks) {
NBookMark tempMark = new NBookMark();
tempMark.setArticleId(Long.toString(item.getArticleId()));
Document object = articleService.getInfo(articleFolderPath, Long.toString(item.getArticleId()));
if(bkMarksource == null) { //this line the object is keep null, cannot pass the null checking
continue;
}
//Do some null checking here, but cannot passed, since the object get null
@Service
public class ArticleService {
public Document getInfo(String articleFolderPath, String articleId) {
Document Document = null;
StandardXStream standardXStream = new StandardXStream();
standardXStream.processAnnotations(Document.class);
String url = "www.testing.com";
try {
standardXStream.setClassLoader(Document.class.getClassLoader());
Document = (Document) standardXStream.readFromUrlandFile(url, filePath, false);
} catch (RuntimeException e) {
logger.info(e);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
if(Document != null) {
return Document;
}else {
return null;
}
}
当... thenReturn()时是否可以使用Mockito。要调用实际功能?
Mockito.when(articleService.getInfo(Mockito.anyString(),Mockito.anyString())).thenReturn(articleService.getInfo("test","test"));
答案 0 :(得分:0)
您没有显示如何创建articleService,所以我只能假定它是一个模拟对象。
未模拟的方法默认情况下不会调用原始方法,它们只会返回null。要使其调用原始方法,您需要做的是:
代替
ArticleService articleService = Mockito.mock(ArticleService.class)
使用参数Mockito.CALLS_REAL_METHODS创建模拟对象
ArticleService articleService = Mockito.mock(ArticleService.class, Mockito.CALLS_REAL_METHODS)