在休息时测试内部服务器错误 api
Test for internal server errors in rest api
我想为我的 REST API spring 引导应用程序的负面场景编写单元测试用例。
控制器方法如下所示:
@RequestMapping(path = "/getcalc/srn/{srn}", method = RequestMethod.GET)
public RestResponse<List<BookMarkMRPostProcessCalc>> fetchLatestPostProcessCalc(@PathVariable("srn") String srn) {
try {
List<BookMarkMRPostProcessCalc> calcList = bookMarkMrPostProcessCalcService.getPostProcessCalc(srn);
return ResponseUtil.prepareRestResponse(calcList);
} catch (BookMarkServiceException e) {
return ResponseUtil.prepareErrorRestResponse(e.getMessage(), "", e.toString());
}
}
积极的情况很好。我想为 BookMarkServiceException
发生或模拟它的场景编写测试用例。我们如何在 junit 中实现这一点?
一种方法如下:
@RunWith(SpringRunner.class)
public class YourClassNameTest {
@InjectMocks
pricvate YourClassName yourClassName;
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Test
public void fetchLatestPostProcessCalcTest(){
expectedException.expect(BookMarkServiceException.class);
yourClassName.fetchLatestPostProcessCalc("input that would generate error");
}
我想为我的 REST API spring 引导应用程序的负面场景编写单元测试用例。
控制器方法如下所示:
@RequestMapping(path = "/getcalc/srn/{srn}", method = RequestMethod.GET)
public RestResponse<List<BookMarkMRPostProcessCalc>> fetchLatestPostProcessCalc(@PathVariable("srn") String srn) {
try {
List<BookMarkMRPostProcessCalc> calcList = bookMarkMrPostProcessCalcService.getPostProcessCalc(srn);
return ResponseUtil.prepareRestResponse(calcList);
} catch (BookMarkServiceException e) {
return ResponseUtil.prepareErrorRestResponse(e.getMessage(), "", e.toString());
}
}
积极的情况很好。我想为 BookMarkServiceException
发生或模拟它的场景编写测试用例。我们如何在 junit 中实现这一点?
一种方法如下:
@RunWith(SpringRunner.class)
public class YourClassNameTest {
@InjectMocks
pricvate YourClassName yourClassName;
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Test
public void fetchLatestPostProcessCalcTest(){
expectedException.expect(BookMarkServiceException.class);
yourClassName.fetchLatestPostProcessCalc("input that would generate error");
}