所以我正在为一个简单的 Post 方法控制器编写一个 Junit。我编写了以下 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"));
}
具有GenerateValue和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;
带有端点的控制器:
@PostMapping("/createCust")
public ResponseEntity<CustomerEntity> 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
原因是你的控制器带有
@Controller
注释。您必须使用 @RestController
注释来使控制器返回 JSON。