I have worked with the intergration test in springboot and i have tried to test a get method in my controller.The method is work in postman.However it fail in test which the Json path is not found.
The tested controller method:
@GetMapping("/product/{id}")
public Product getproduct(@PathVariable int id){
return productRepository.findById(id).orElse(null);
}
My test code:
@ExtendWith(SpringExtension.class)
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private ProductRepository productRepository;
@MockBean
private CustomerRepository customerRepository;
@Test
void getproduct() throws Exception {
/* RequestBuilder request= MockMvcRequestBuilders.;*/
mvc.perform(get("/product/{pid}","201")).andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.productName").value("Mobile"));
}
}
result in postman :
{
"pid": 201,
"productName": "Mobile",
"quantity": 1,
"price": 20000
}
error message:
java.lang.AssertionError: No value at JSON path "$.productName"