Ошибка теста JUnit: java.lang.AssertionError: нет значения в пути JSON «$.id» для «andExpect»JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Ошибка теста JUnit: java.lang.AssertionError: нет значения в пути JSON «$.id» для «andExpect»

Сообщение Anonymous »

Итак, я пишу Junit для простого контроллера метода Post. и я написал следующий Junit, и он выдает ошибку:
У меня есть тест,
Это написанный мной код Jnit: Что-то выглядит не так?
@Test
public void testCreateCar() throws Exception {

String custJson = "{\"name\": \"John Doe\", " +
"\"street\": \"123 Main St\", " +
"\"house_No\": \"45B\", " +
"\"place\": \"Springfield\", " +
"\"email\": \"[email protected]\", " +
"\"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("[email protected]"))
.andExpect(MockMvcResultMatchers.jsonPath("$.ph_Number").value("1234567890"));
}

Класс сущности с GeneratedValue и идентификатором
@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;

Контроллер с конечной точкой:

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

Это мой простой сервис:
public CustomerEntity createCust(CustomerEntity cust) {
return custRepo.save(cust);
}

Что не так с тестом?
Ошибка:
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


Подробнее здесь: https://stackoverflow.com/questions/790 ... -id-for-an
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»