我正在编写单元测试来测试控制器。但是,我尝试在... andReturn(true)时使用Mockito使函数返回true,但我仍然无法通过测试。
@Test
public void testingFunc() throws Exception{
Mockito.when(testingService.create(userId, deviceId, requestIP, userAgent,"test","test")).thenReturn(true);
MvcResult result = mockMvc.perform(post("/test")
.param("id","testing")
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
String response = result.getResponse().getContentAsString();
assertEquals("success", response);
}
@RequestMapping(method = RequestMethod.POST, value="/test")
@ResponseBody
public String response(String id){
boolean result = testingService.create(userId, Base64.encodeBase64URLSafeString(deviceId.getBytes()),
request.getRemoteAddr(), Base64.encodeBase64URLSafeString(userAgent.toString().getBytes(Charset.forName("UTF-8"))), "test", "test");
if(result) return "success";
return "fail";
}