lundi 18 juin 2018

Post request with mockvc HttpMessageNotReadableException

I am trying to do some integration testing on my application, but encountered some difficulties with sending POST request using mockvc in Spring.

Here is method I am trying to test:

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

here is how I setup my integration testing environment:

@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()
   }
}

and the test itself:

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))
}
}

Model which I created and pass to method:

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

   @NonNull
   DriverType driverType;
}

When I make explicit POST with curl or Postman it is working just fine, but when I am trying to make it in test I get:

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)

I don't think so I'm doing anything wrong here, but maybe there is something I am missing, any advice would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire