当我像这样运行 JUnit 测试时:
@Test
public void insertSignRewardTest() throws Exception{
SignRewardRequest srr = new SignRewardRequest();
srr.setBeginTime(new Date().toLocaleString());
srr.setPoint(21);
srr.setUseableId("123");
srr.setEndTime(new Date().toLocaleString());
SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
PointRewardDeploy pointRewardDepoly = new PointRewardDeploy();
Date reward_begin_date = new Date();
Date reward_end_date = new Date();
reward_begin_date = sFormat.parse(srr.getBeginTime());
reward_end_date = sFormat.parse(srr.getEndTime());
pointRewardDepoly.setPoint_source_type(Long.valueOf(PointSourceType.SIGNED.getValue().toString()));
pointRewardDepoly.setCreate_time(new Date());
pointRewardDepoly.setUpdate_time(new Date());
pointRewardDepoly.setReward_begin_date(reward_begin_date);
pointRewardDepoly.setReward_end_date(reward_end_date);
pointRewardDepoly.setStatus(1l);
pointRewardDepoly.setReward_point(srr.getPoint());
List<PointRewardDeploy> listPointRewardDeploy = new ArrayList<PointRewardDeploy>();
listPointRewardDeploy.add(pointRewardDepoly);
CommonListResult<PointRewardDeploy> cr = new CommonListResult<PointRewardDeploy>();
cr.setCode("success");
cr.setMsg("ok");
cr.setSuccess(true);
List<PointRewardDeploy> date = new ArrayList<>();
date.add(pointRewardDepoly);
cr.setData(date);
ObjectMapper mapper = new ObjectMapper();
String str = mapper.writeValueAsString(srr);
when(pointAccountService.savePointRewardDeploy(listPointRewardDeploy)).thenReturn(cr);
this.mockMvc.perform(MockMvcRequestBuilders.post("/api/sign/signPrizeDraw/insertSignReward")
.contentType(MediaType.APPLICATION_JSON)
.content(str))
.andDo(print())
.andExpect(status().isOk());
Assert.assertEquals(true, cr.getSuccess());
verify(pointAccountService,times(1)).savePointRewardDeploy(refEq(listPointRewardDeploy));
}
我在以下位置使用org.mockito.Matchers.refEq
:
verify(pointAccountService,times(1))
.savePointRewardDeploy(listPointRewardDeploy)
verify(pointAccountService,times(1))
.savePointRewardDeploy(refEq(listPointRewardDeploy))
然后运行它,它通过了测试,但打印输出消息为空,例如:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /api/sign/signPrizeDraw/insertSignReward
Parameters = {}
Headers = {Content-Type=[application/json]}
Handler:
Type = com.yirendai.mplatform.sign.controller.UserInfoListController
Method = public
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
我的问题:打印消息是否正确?我认为这是不正确的,但不知道如何解决它。谢谢你帮助我。
打印输出对我来说看起来是正确的,但是如果不看到映射到/api/sign/signPrizeDraw/insertSignReward
的控制器方法,就很难确定,特别是:该方法返回的内容。
如果您希望body
包含在打印输出中,则:
- 请求中未打印
body
- 仅当响应包含
body
时,响应中才会打印body
请记住,print()
不是 MVC 层的正式输出,它只是一个(有时(有用的诊断工具。只要你测试...
- 状态代码
- 响应内容和类型(如果控制器方法返回某些内容(
- 任何副作用,以对控制器下方层的验证或期望的形式(例如服务或存储库调用(
。那么我认为您的 MockMvc 测试用例就足够了,您无需关心print()
输出的结果。