无法使用 Spring MockMvc 将正文附加到我的 POST 请求
Can not attach body to my POST request using Spring MockMvc
我正在尝试测试我的休息控制器。 GET 没有问题,但是当我尝试测试 POST 方法时,我无法附加正文。
private static final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));
private ObjectMapper jsonMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
@Test
public void test1() throws Exception {
//...Create DTO
//...Create same pojo but as entity
when(serviceMock.addEntity(e)).thenReturn(e);
mvc.perform(post("/uri")
.contentType(contentType)
.content(jsonMapper.writeValueAsString(dto))
)
.andDo(print())
.andExpect(status().isCreated())
.andExpect(content().contentType(contentType)); //fails because there is no content returned
}
这是请求输出:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /uri
Parameters = {}
Headers = {Content-Type=[application/json;charset=UTF-8]}
没有尸体。为什么?我打印了 jsonMapper.writeValueAsString(dto)
并且不为空。
编辑:
添加控制器代码:
@RestController
@RequestMapping("/companies")
public class CompanyController {
@Autowired
private CompanyService service;
@Autowired
private CompanyMapper mapper;
@RequestMapping(method=RequestMethod.GET)
public List<CompanyDTO> getCompanies() {
List<Company> result = service.getCompanies();
return mapper.toDtoL(result);
}
@RequestMapping(method=RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public CompanyDTO createCompany(@RequestBody @Valid CompanyDTO input) {
Company inputE = mapper.toEntity(input);
Company result = service.addCompany(inputE);
return mapper.toDto(result);
}
已解决。
模拟调用应使用 any
而不是具体对象:when(serviceMock.addCompany(any(Company.class))).thenReturn(e);
我需要覆盖实体 class 的 equals 方法才能传递此语句:verify(serviceMock, times(1)).addCompany(e);
我正在尝试测试我的休息控制器。 GET 没有问题,但是当我尝试测试 POST 方法时,我无法附加正文。
private static final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));
private ObjectMapper jsonMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
@Test
public void test1() throws Exception {
//...Create DTO
//...Create same pojo but as entity
when(serviceMock.addEntity(e)).thenReturn(e);
mvc.perform(post("/uri")
.contentType(contentType)
.content(jsonMapper.writeValueAsString(dto))
)
.andDo(print())
.andExpect(status().isCreated())
.andExpect(content().contentType(contentType)); //fails because there is no content returned
}
这是请求输出:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /uri
Parameters = {}
Headers = {Content-Type=[application/json;charset=UTF-8]}
没有尸体。为什么?我打印了 jsonMapper.writeValueAsString(dto)
并且不为空。
编辑:
添加控制器代码:
@RestController
@RequestMapping("/companies")
public class CompanyController {
@Autowired
private CompanyService service;
@Autowired
private CompanyMapper mapper;
@RequestMapping(method=RequestMethod.GET)
public List<CompanyDTO> getCompanies() {
List<Company> result = service.getCompanies();
return mapper.toDtoL(result);
}
@RequestMapping(method=RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public CompanyDTO createCompany(@RequestBody @Valid CompanyDTO input) {
Company inputE = mapper.toEntity(input);
Company result = service.addCompany(inputE);
return mapper.toDto(result);
}
已解决。
模拟调用应使用
any
而不是具体对象:when(serviceMock.addCompany(any(Company.class))).thenReturn(e);
我需要覆盖实体 class 的 equals 方法才能传递此语句:
verify(serviceMock, times(1)).addCompany(e);