【发布时间】:2018-07-04 23:05:24
【问题描述】:
所以这是我在代码中所面临的场景和问题
// the call that I am making in my test, please note that myService is a Mocked object
Foo foo = new Foo();
when(myService.postFoo(foo)).thenReturn(true);
mockMvc.perform(post("/myEndpoint")
.contentType(APPLICATION_JSON_UTF8)
.content(toJsonString(foo))
.andExpect(status().isAccepted());
// this is the controller method that get's called
@PostMapping("/myEndpoint")
@ResponseStatus(code = HttpStatus.ACCEPTED)
public String postFoo(@RequestBody Foo foo) {
if (myService.postFoo(foo)) {
return "YAY";
}
return "" + 0 / 0;
}
我面临的问题是 mockMvc 的 post 传入的 foo 是 Foo 的一个新实例,所以 myService.postFoo(foo) 的 if 语句失败。我假设引擎使用我的 foo 对象的 jsonString 来创建一个新的,它在字段方面是相同的,但是是一个不同的对象,因此使“if”语句失败。
我该如何解决这个问题?
【问题讨论】:
标签: spring spring-mvc spring-boot mockito mockmvc