Java JUnit5 MockMvc问题-返回空正文

Java JUnit5 MockMvc问题-返回空正文,java,rest,controller,junit5,mockmvc,Java,Rest,Controller,Junit5,Mockmvc,我对MockMvc有问题。我正在尝试测试我的REST控制器post方法(见下文),结果是空体。怎么了,你知道吗 这是上课的时间 @EqualsAndHashCode @Value @Builder public class CustomerCreateDTO { @JsonCreator public CustomerCreateDTO(@JsonProperty("name") String name, @JsonProperty("idax&q

我对MockMvc有问题。我正在尝试测试我的REST控制器post方法(见下文),结果是空体。怎么了,你知道吗

这是上课的时间


@EqualsAndHashCode
@Value
@Builder
public class CustomerCreateDTO {
    @JsonCreator
    public CustomerCreateDTO(@JsonProperty("name") String name, @JsonProperty("idax") String idax) {
        this.name = name;
        this.idax = idax;
    }

    @NotNull(message = MessageContent.VALID_NOT_NULL)
    @NotBlank(message = MessageContent.VALID_NOT_BLANK)
    @Size(max = 60, message = MessageContent.VALID_MAX_SIZE + 60)
    String name;

    @Unique(fieldName = "idax", handler = CustomerService.class, message = MessageContent.VALID_UNIQUE)
    @NotNull(message = MessageContent.VALID_NOT_NULL)
    @NotBlank(message = MessageContent.VALID_NOT_BLANK)
    @Size(max = 4, message = MessageContent.VALID_MAX_SIZE + 4)
    String idax;
}
波乔班

@Data
@Builder
@AllArgsConstructor
@Entity
@Table(name = "customers")
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 60)
    private String name;

    @Column(length = 4, nullable = false, unique = true)
    private String idax;

    public Customer() {
        //JPA Only
    }
}

以下是控制器(部分):


就像你看到的,尸体是空的,所以我不能检查反应数据。有人知道问题出在哪里吗?有什么建议吗?提前感谢。

关于
何时(customerService.create(customerCreateDTO)),然后返回(customer)
-->您确定调用的对象与
customerCreateDTO
相同(例如引用相等)还是包含与
customerCreateDTO
相同数据的对象?在后者中,尝试
eq(customerCreateDTO)
您正在模拟
fieldstompconverter
,但不中断方法调用。Arho-是的,我找到了答案。不过还是谢谢你。
@RequiredArgsConstructor
@RestController
@RequestMapping("/customers")
public class CustomerController {
    private final CustomerService customerService;
    private final FieldsToMapConverter<ResponseDetails> fieldsToMapConverter;
    private final PageAdvice pageAdvice;
    private final AppConfig appConfig;

@PostMapping ()
    public ResponseEntity<Map<String, Object>> create(@Valid @RequestBody CustomerCreateDTO customerCreateDTO) {
        var customer = customerService.create(customerCreateDTO);
        var location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(customer.getId())
                .toUri();

        return ResponseEntity.created(location).body(fieldsToMapConverter.getFieldsAsMap(
                ResponseDetails.builder()
                        .message(MessageContent.OK)
                        .data(CustomerDTOMapper.entityToDtoOnlyId(customer))
                        .location(location)
                        .build()));
    }
@WebMvcTest(controllers = CustomerController.class)
class CustomerControllerTest {
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private MockHttpSession session;
    @Autowired
    private ObjectMapper objectMapper;
    @MockBean
    private CustomerService customerService;
    @MockBean
    private FieldsToMapConverter<ResponseDetails> fieldsToMapConverter;
    @MockBean
    private PageAdvice pageAdvice;
    @MockBean
    private AppConfig appConfig;
    @MockBean
    private CustomerRepository customerRepository;

    @Test
    @DisplayName("POST /customers")
    void testCreateCustomer() throws Exception {
        CustomerCreateDTO customerCreateDTO = new CustomerCreateDTO("Test", "4444");
        Customer customer = new Customer();
        customer.setIdax("4444");
        customer.setName("Test");
        customer.setId(1L);
        when(customerService.create(customerCreateDTO)).thenReturn(customer);

        mockMvc.perform(post("/customers")
                .content(objectMapper.writeValueAsString(customerCreateDTO))
                .contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(status().isCreated())
                .andExpect(header().string(HttpHeaders.LOCATION, "http://localhost/customers/1"))
                .andExpect(jsonPath("$.id", is("1")));
    }
MockHttpServletResponse:
           Status = 201
    Error message = null
          Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", Location:"http://localhost/customers/1", Content-Type:"application/json"]
     Content type = application/json
             Body = {}
    Forwarded URL = null
   Redirected URL = http://localhost/customers/1
          Cookies = []