在具有相同请求主体的一个测试方法中有两个postForEntity时,Spring引导Web集成测试失败了吗?

时间:2015-12-27 06:03:54

标签: spring spring-boot integration-testing

Spring启动项目,什么时候做过web集成测试遇到问题,很奇怪。业务逻辑很简单,只需要统计预订。

控制器:

@RequestMapping(value="/plus1",method=RequestMethod.POST)
public ResponseEntity<?> addReservation(@RequestBody ReservationDTO dto){
    logger.info("{} plus 1", dto.getName());
    //check if has plused
    Reservation reservation = reservationDao.findByNameAndCreatedDate(dto.getName(),LocalDate.now());
    if(reservation != null){
        return ResponseEntity.badRequest().body(String.format("%s %s has reserved", dto.getName(),LocalDate.now().toString()));
    }
    //if today not reserved plus one
    Reservation entity = BeanMapper.map(dto, Reservation.class);
    entity.setCreatedDate(LocalDate.now());
    reservationDao.save(entity);

    return ResponseEntity.ok(entity);
}

Web集成测试:

@Test
public void test_repeat_add_reservation(){
    String add_reservation_url = String.format(add_reservation_url_format, port);
    ReservationDTO dto = new ReservationDTO();
    dto.setName("foo");
    dto.setComment("bar");
    new TestRestTemplate().postForEntity(add_reservation_url, dto , Reservation.class);
    new TestRestTemplate().postForEntity(add_reservation_url, dto , Reservation.class);
}

执行此测试时,它具有以下异常:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'foo': was expecting 'null', 'true', 'false' or NaN
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@51b87df7; line: 1, column: 5]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'foo': was expecting 'null', 'true', 'false' or NaN
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@51b87df7; line: 1, column: 5]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:224)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:208)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:835)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:819)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:599)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:384)

为什么如果两个postForEntity连续具有相同的请求对象,它会抛出异常? 现在我稍微更改了代码,现在没问题。

    ReservationDTO dto = new ReservationDTO();
    dto.setName("foo");
    dto.setComment("bar");
    new TestRestTemplate().postForEntity(add_reservation_url, dto , Reservation.class);
    dto.setName("foo2");
    new TestRestTemplate().postForEntity(add_reservation_url, dto , Reservation.class);

1 个答案:

答案 0 :(得分:0)

我犯了一个愚蠢的错误。

new TestRestTemplate().postForEntity(add_reservation_url, dto , Reservation.class);
new TestRestTemplate().postForEntity(add_reservation_url, dto , Reservation.class);

}

因为第二篇文章只返回

return ResponseEntity.badRequest().body(String.format("%s %s has reserved", dto.getName(),LocalDate.now().toString()));

所以第二篇文章应该改为

new TestRestTemplate().postForEntity(add_reservation_url, dto , String.class);