【发布时间】:2020-07-15 17:45:38
【问题描述】:
我编写了以下代码,mockmvc.perform 不会捕获异常,而是返回错误堆栈。我使用调试器确认控制器抛出了正确的错误。我是 SpringBoot 新手,不明白为什么测试控制器没有处理异常。以下是我的测试控制器,它对外部服务进行了三个 Api 调用。控制器返回异常,但 Mockmvc.perform 无法断言它。
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { Application.class, ApplicationTest.class })
@AutoConfigureMockMvc
@ContextConfiguration(initializers = {WireMockInitializer.class})
public class myControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Autowired private WireMockServer wireMockServer;
@Autowired
private myController myController;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(myController)
.build();
}
@Test
@DisplayName("Should Return Execution Error")
public void shouldReturnExecutionErrorOnService() throws Exception {
// Making Three Api calls the controller internally invokes them
configureStubA(HttpStatus.INTERNAL_SERVER_ERROR, args, "invalidResponse.json");
configureStubB(HttpStatus.INTERNAL_SERVER_ERROR, args, args2, args3,
"invalidResponse.json");
configureStubC(HttpStatus.INTERNAL_SERVER_ERROR, args, "invalidResponse.json");
mockMvc
.perform(
get("/something")
.param("a", a)
.param("b", b)
.param("c", c)
.param("d", d)
.param("e", e.toArray(new String[] {})))
.andDo(print())
.andExpect(status().is5xxServerError())
.andExpect(result -> assertTrue(result.getResolvedException() instanceof
IllegalStateException));
}}
【问题讨论】:
标签: spring-boot mocking mockito integration