400 BAD_REQUEST. Can't catch Post request from RestTemplate.PostForEntity() call

Viewed 13

I have a Test method for testing to save a one-to-many cascade data into two tables: Customer and Address, but the controller is not able to catch the Post call in this cascade testing, however the controller can Post receive call from posting data only for the Customer table, without data for the ShippingAddress.

Here is the test case for posting cascade data for the Customer and ShippingAddress tables: @Test public void create_shipping_address_and_customer() throws Exception {

    Customer newCustomer = new Customer("Alex", "Maxim", "alexmaxim@gmail.com", "43323222222");
    ShippingAddress shippingAddress1 = new ShippingAddress("unit 1", "89 hanson road", "New York", "CA", "KKKDLDL");
    newCustomer.addShippingAddress(shippingAddress1);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Customer> requestEntity = new HttpEntity<>(newCustomer, headers);
    ResponseEntity<Customer> response = restTemplate.postForEntity("/customer/add", requestEntity, Customer.class);
    assertEquals(HttpStatus.CREATED, response.getStatusCode());

}

Error is: [ERROR] Failures: [ERROR] ShippingAddressControllerRestTemplateTest.create_shipping_address_and_customer:85 expected:<201 CREATED> but was:<400 BAD_REQUEST>

I have been stuck in this issue for more than a week, have tried to use exchange() method, still failed. Could anyone shred some light to me ? Thanks

0 Answers
Related