POST spring 中的方法测试。代码 400 而不是 201

POST method test in spring. Code 400 instead of 201

我正在创建一个可以创建汽车的应用程序 object。 没有 setter 和 getter 的汽车 class:

package github.KarolXX.demo.model;


import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;

@Entity
@Table(name = "cars")
public class Car {
    @Id
    @GeneratedValue(generator = "inc")
    @GenericGenerator(name = "inc", strategy = "increment")
    private int id;
    @NotBlank(message = "car name`s must be not empty")
    private String name;
    private LocalDateTime productionYear;
    private boolean tested;

    public Car() {
    }

    public Car(@NotBlank(message = "car name`s must be not empty") String name, LocalDateTime productionYear) {
        this.name = name;
        this.productionYear = productionYear;
    }
}

我想知道如何测试 Spring 中的 POST 方法。下面是 POST 方法的代码片段,它只创建 Java object 命名为 Car(第一个片段)

 @PostMapping("/cars")
ResponseEntity<Car> createCar(@RequestBody @Valid Car newCar) {
    logger.info("Creating new car");
    var result = repository.save(newCar);
    return ResponseEntity.created(URI.create("/" + result.getId())).body(result);
}

我正在尝试以这种方式进行测试:

package github.KarolXX.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import github.KarolXX.demo.TestConfiguration;
import github.KarolXX.demo.model.Car;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.time.LocalDateTime;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("integration")
class CarControllerIntegrationServerSideTest {
    @Autowired
    private MockMvc mockMvc;    

    @Test
    public void httpPost_createsNewCar_returnsCreatedCar() throws Exception {
        //given
        Car car = new Car("second server side test", LocalDateTime.parse("2021-01-02T13:34:54"));

        //when + then
        mockMvc.perform(post("/cars")
                .content(asJsonString(car))
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(status().isCreated());
    }

    public static String asJsonString(final Car objectCar) {
        try {
            return new ObjectMapper().writeValueAsString(objectCar);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}



   

我是根据这个做的article 不幸的是我的测试没有通过。虽然在日志中我可以看到请求 body 和请求 headers 设置正确,但我得到状态 400

2021-03-26 12:05:01.532  WARN 10696 --- [           main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Expected array or string.; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string.
 at [Source: (PushbackInputStream); line: 1, column: 59] (through reference chain: github.KarolXX.demo.model.Car["productionYear"])]

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /cars
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json", Content-Length:"279"]
             Body = {"id":0,"name":"second server side test","productionYear":{"month":"JANUARY","dayOfWeek":"SATURDAY","dayOfYear":2,"nano":0,"year":2021,"monthValue":1,"dayOfMonth":2,"hour":13,"minute":34,"second":54,"chronology":{"id":"ISO","calendarType":"iso8601"}},"tested":false,"brand":null}
    Session Attrs = {}

Handler:
             Type = github.KarolXX.demo.controller.CarController
           Method = github.KarolXX.demo.controller.CarController#createCar(Car)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = org.springframework.http.converter.HttpMessageNotReadableException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 400
    Error message = null
          Headers = []
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /cars
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json", Content-Length:"279"]
             Body = {"id":0,"name":"second server side test","productionYear":{"month":"JANUARY","dayOfWeek":"SATURDAY","dayOfYear":2,"nano":0,"year":2021,"monthValue":1,"dayOfMonth":2,"hour":13,"minute":34,"second":54,"chronology":{"id":"ISO","calendarType":"iso8601"}},"tested":false,"brand":null}
    Session Attrs = {}

Handler:
             Type = github.KarolXX.demo.controller.CarController
           Method = github.KarolXX.demo.controller.CarController#createCar(Car)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = org.springframework.http.converter.HttpMessageNotReadableException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 400
    Error message = null
          Headers = []
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Status expected:<201> but was:<400>
Expected :201
Actual   :400

我不知道哪里出了问题,但是当我将这行代码 mockMvc.perform(post("/cars") 更改为这一行时 mockMvc.perform(post("/") 然后我得到了状态 404

我已经解决了问题,但不知道为什么以前的版本不能用。我从测试 class 中删除了静态方法并更改了 POST 方法检查:即我创建了一个将 JSON 作为字符串保存的变量并将其传递给 content 方法。在以前的版本中,此字符串由静态方法中的 ObjectMapper().writeValueAsString() 返回。我修改后的测试 class:

@Test
public void httpPost_createsNewCar_returnsCreatedCar() throws Exception {
    //given
    //Car car = new Car("second server side test", LocalDateTime.parse("2021-01-02T13:34:54"));
    String id = String.valueOf(repo.getSize());
    String jsonString = new JSONObject()
            .put("id", id)
            .put("tested", false)
            .put("productionYear", "2017-06-18T12:12:12")
            .put("name", "Toyota")
            .toString();

    //when + then
    mockMvc.perform(post("/cars")
            .content(jsonString)
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON))
            .andDo(MockMvcResultHandlers.print())
            .andExpect(status().isCreated());
}