ฉันมีตัวควบคุม RESTful ที่เรียบง่ายซึ่งสร้าง json และฉันต้องการทดสอบว่ามันใช้งานได้ดีหรือไม่ ฉันสามารถทดสอบวิธีการได้อย่างง่ายดายเมื่อมีรหัสผู้ใช้อยู่ โดยไม่มีปัญหา ฉันต้องการตรวจสอบว่า IllegalArgumentException ถูกส่งออกไปเมื่อไม่มีรหัสที่ระบุ มีวิธีใดบ้างที่จะทำเช่นนั้น?
ในแอปพลิเคชัน เมื่อเกิดข้อผิดพลาดในการยืนยัน การตอบสนองจะถูกเปลี่ยนเส้นทางไปยังหน้าข้อผิดพลาดทั่วไปที่แสดงข้อความทั่วไป ถ้าฉันใช้ response(withStatus(HttpStatus.NOT_FOUND) ฉันไม่ได้ตรวจสอบข้อยกเว้นหรือข้อความของฉัน ใครช่วยฉันหน่อยได้ไหม
นี่คือวิธีที่ฉันต้องการทดสอบ:
@Transactional (readOnly = true)
@RequestMapping(method=RequestMethod.GET, value="/json/user/{id}", headers="Accept=application/json, text/html")
public @ResponseBody UserData getUserJson(@PathVariable Long id)
{
User user = this.userService.findUserById(id);
Assert.notNull(user , "User does not exist with the given id: " + id);
return new UserData (user);
}
นี่คือการทดสอบที่มีรหัสที่ถูกต้อง:
@Test
public void testRestUserJson() {
mockServer.expect(requestTo(JSON_URL + USER)).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(JSON_RESPONSE, MediaType.APPLICATION_JSON));
UserData userData = restClientTemplate.getForObject(JSON_URL + USER, UserData.class);
AssertResponseContent(userData , expectedData);
mockServer.verify();
EasyMock.verify(user);
}
ขอแสดงความนับถือ ดาเนียลา