使用mockvc HttpMessageNotReadableException发布请求

时间:2018-06-18 10:41:31

标签: spring-boot testing integration-testing spock mockmvc

我正在尝试对我的应用程序进行一些集成测试,但是在Spring中使用mockvc发送POST请求时遇到了一些困难。

这是我试图测试的方法:

@PostMapping(value = "/driver/start")
public ReservationDto startParkmeter(@RequestBody CreateReservationCommand createReservationCommand)
{
    return reservationFacade.startParkmeter(createReservationCommand);
}

以下是我设置集成测试环境的方法:

@TypeChecked
@SpringBootTest(classes = [ParkingLotApplication])
@ActiveProfiles([Profiles.TEST])
@Transactional
@Rollback
abstract class IntegrationSpec extends Specification
{
   @Autowired
   protected WebApplicationContext webApplicationContext

   MockMvc mockMvc

   @Before
   void setupMockMvc()
   {
      mockMvc = MockMvcBuilders
          .webAppContextSetup(webApplicationContext)
          .build()
   }
}

和测试本身:

class ReservationControllerAcceptanceSpec extends IntegrationSpec implements SampleReservations
{
    def "should show valid path for driver"()
    {
        given:"given system is completely empty"

        when:"driver starts park meter"

        ResultActions startReservation = mockMvc.perform(post("/parking/driver/start", createReservationCommand))
}
}

我创建的模型并传递给方法:

@Data
@Builder
public class CreateReservationCommand
{
   @NonNull
   String carLicenseId;

   @NonNull
   DriverType driverType;
}

当我使用curl或Postman进行显式POST时,它工作正常,但当我尝试在测试中进行时,我得到:

2018-06-18 12:29:22.003  WARN 17236 --- [           main] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.stosik.parking.reservation.dto.ReservationDto com.stosik.parking.ParkingController.startParkmeter(com.stosik.parking.reservation.dto.CreateReservationCommand)

我不这么认为,我在这里做错了什么,但也许有一些我想念的东西,任何建议都会非常感激。

0 个答案:

没有答案