【问题标题】:Test for internal server errors in rest api在 rest api 中测试内部服务器错误
【发布时间】:2019-11-15 20:29:42
【问题描述】:

我想为我的 REST API Spring Boot 应用程序编写负面场景的单元测试用例。

控制器方法如下所示:

@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中实现这一点?

【问题讨论】:

    标签: junit4 spring-test spring-test-mvc


    【解决方案1】:

    一种方法如下:

    @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");
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 2012-05-29
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 2018-06-20
      • 1970-01-01
      • 2016-07-08
      相关资源
      最近更新 更多