当andReturn无法返回预期结果时的Mockito

时间:2020-09-25 02:44:27

标签: mockito

我正在编写单元测试来测试控制器。但是,我尝试在... 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";
}

0 个答案:

没有答案