Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3630

JUnit test error: java.lang.AssertionError: No value at JSON path "$.id" for 'andExpect'

$
0
0

So I am writing a Junit for a simple Post method controller. and I wrote the following Junit and it is throwing an error:I have the test here,This is my Jnit code written: Does anything look wrong?

@Test    public void testCreateCar() throws Exception {      String custJson = "{\"name\": \"John Doe\", " +"\"street\": \"123 Main St\", " +"\"house_No\": \"45B\", " +"\"place\": \"Springfield\", " +"\"email\": \"john.doe@example.com\", " +"\"ph_Number\": \"1234567890\"}";    CustomerEntity customer = new CustomerEntity();    customer.seters except ID...    CustomerEntity customer2 = new CustomerEntity();    customer2.setters.....    given(custService.createCust(customer)).willReturn(customer2);    mvc.perform(MockMvcRequestBuilders.post("/api/createCust")        .contentType(MediaType.APPLICATION_JSON).content(custJson))        .andExpect(MockMvcResultMatchers.status().isOk())        .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1))        .andExpect(MockMvcResultMatchers.jsonPath("$.name").value("John Doe"))        .andExpect(MockMvcResultMatchers.jsonPath("$.street").value("123 Main St"))        .andExpect(MockMvcResultMatchers.jsonPath("$.house_No").value("45B"))        .andExpect(MockMvcResultMatchers.jsonPath("$.place").value("Springfield"))        .andExpect(MockMvcResultMatchers.jsonPath("$.email").value("john.doe@example.com"))        .andExpect(MockMvcResultMatchers.jsonPath("$.ph_Number").value("1234567890"));    }

Entity Class with GeneratedValue and ID

 @Entity      public class CustomerEntity {    public CustomerEntity(Integer id, String name, String street, String house_No, String place,      @Email String email, @Pattern(regexp = "(^$|[0-9]{10})") String ph_Number) {    super();    this.id = id;    this.name = name;    this.street = street;    this.house_No = house_No;    this.place = place;    this.email = email;    this.ph_Number = ph_Number;    }//constructor    public CustomerEntity() {}    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private Integer id;    private String name;    private String street;    private String house_No;    private String place;    @Email    private String email;    @Pattern(regexp = "(^$|[0-9]{10})")    private String ph_Number;

Controller with endpoint:

    @PostMapping("/createCust")    public ResponseEntity<CustomerEntity> createCust(@Valid @RequestBody CustomerEntity cust) {    CustomerEntity customer = custService.createCust(cust);    return ResponseEntity.status(HttpStatus.OK).body(customer);    }

This is my simple Service:

    public CustomerEntity createCust(CustomerEntity cust) {    return custRepo.save(cust);    }

What is wrong here with the test?

Error:

     java.lang.AssertionError: No value at JSON path "$.id"  at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:302) at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:99) at org.springframework.test.web.servlet.result.JsonPathResultMatchers.lambda$value$2(JsonPathResultMatchers.java:111) at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:214) at com.rupesh.assesment.carlease.controller.CustomerControllerTest.testCreateCar(CustomerControllerTest.java:57) at java.base/java.lang.reflect.Method.invoke(Method.java:580)  at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)  at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)  Caused by: java.lang.IllegalArgumentException: json can not be null or empty  at com.jayway.jsonpath.internal.Utils.notEmpty(Utils.java:401)  at com.jayway.jsonpath.JsonPath.read(JsonPath.java:390)  at com.jayway.jsonpath.JsonPath.read(JsonPath.java:377)  at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:299) ... 7 more

Viewing all articles
Browse latest Browse all 3630

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>