【问题标题】:how to get bottom error message from a nested exception using mockmvc如何使用 mockmvc 从嵌套异常中获取底部错误消息
【发布时间】:2019-05-31 17:03:02
【问题描述】:

我的 Junit 测试出现如下嵌套异常:

 Caused by: com.exception.Test1RestException: operation failed for test1 
at com.testController.tests(Test1Controller.java:123)

.......................

  Caused by: com.exception.Test2Exception: operation failed for test2 
at com.testController.tests(Test2Controller.java:456)

..................

 Caused by: com.exception.Test3RestException: operation failed for test3 
at com.testController.tests(Test2Controller.java:789)

我想使用 mockmvc 验证“test3 操作失败”,在下面使用

代码

org.assertj.core.api.Assertions
        .assertThatThrownBy(() -> 
 mockMvc.perform(fileUpload("/test/abc").file((MockMultipartFile) files))
              .andExpect(status().isOk()))
        .hasMessageContaining(operation failed for test3)

但我收到了预期的错误消息“操作失败 test1",任何指针都可以从我的 junit 到达底部消息

【问题讨论】:

    标签: java spring-mvc junit mockito mockmvc


    【解决方案1】:

    它通过使用 .hasStackTraceContaining(message) 方法调用来工作

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 2014-08-11
      • 2022-01-23
      • 2011-05-26
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      相关资源
      最近更新 更多