27

我想断言引发了异常并且服务器返回 500 内部服务器错误。

为了突出显示意图,提供了一个代码片段:

thrown.expect(NestedServletException.class);
this.mockMvc.perform(post("/account")
            .contentType(MediaType.APPLICATION_JSON)
            .content(requestString))
            .andExpect(status().isInternalServerError());

当然,我写isInternalServerError或写无关紧要isOkthrow.except无论语句下方是否引发异常,测试都将通过。

你打算如何解决这个问题?

4

5 回答 5

12

如果您有异常处理程序并且想要测试特定异常,您还可以断言该实例在已解决的异常中有效。

.andExpect(result -> assertTrue(result.getResolvedException() instanceof WhateverException))
于 2020-04-03T16:58:51.240 回答
12

您可以获得对MvcResult和可能已解决的异常的引用,并检查一般的 JUnit 断言......

MvcResult result = this.mvc.perform(
        post("/api/some/endpoint")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(someObject)))
        .andDo(print())
        .andExpect(status().is4xxClientError())
        .andReturn();

Optional<SomeException> someException = Optional.ofNullable((SomeException) result.getResolvedException());

someException.ifPresent( (se) -> assertThat(se, is(notNullValue())));
someException.ifPresent( (se) -> assertThat(se, is(instanceOf(SomeException.class))));
于 2019-07-02T15:34:24.043 回答
8

您可以尝试以下方法 -

  1. 创建自定义匹配器

    public class CustomExceptionMatcher extends
    TypeSafeMatcher<CustomException> {
    
    private String actual;
    private String expected;
    
    private CustomExceptionMatcher (String expected) {
        this.expected = expected;
    }
    
    public static CustomExceptionMatcher assertSomeThing(String expected) {
        return new CustomExceptionMatcher (expected);
    }
    
    @Override
    protected boolean matchesSafely(CustomException exception) {
        actual = exception.getSomeInformation();
        return actual.equals(expected);
    }
    
    @Override
    public void describeTo(Description desc) {
        desc.appendText("Actual =").appendValue(actual)
            .appendText(" Expected =").appendValue(
                    expected);
    
    }
    }
    
  2. 在 JUnit 类中声明一个@Rule如下 -

    @Rule
    public ExpectedException exception = ExpectedException.none();
    
  3. 在测试用例中使用自定义匹配器 -

    exception.expect(CustomException.class);
    exception.expect(CustomException
            .assertSomeThing("Some assertion text"));
    this.mockMvc.perform(post("/account")
        .contentType(MediaType.APPLICATION_JSON)
        .content(requestString))
        .andExpect(status().isInternalServerError());
    

PS:我提供了一个通用的伪代码,您可以根据您的要求进行自定义。

于 2013-05-17T12:17:55.157 回答
0

我最近遇到了同样的错误,我没有使用 MockMVC,而是创建了一个集成测试,如下所示:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = { MyTestConfiguration.class })
public class MyTest {
    
    @Autowired
    private TestRestTemplate testRestTemplate;
    
    @Test
    public void myTest() throws Exception {
        
        ResponseEntity<String> response = testRestTemplate.getForEntity("/test", String.class);
        
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode(), "unexpected status code");
        
    }   
}

@Configuration
@EnableAutoConfiguration(exclude = NotDesiredConfiguration.class)
public class MyTestConfiguration {
    
    @RestController
    public class TestController {
        
        @GetMapping("/test")
        public ResponseEntity<String> get() throws Exception{
            throw new Exception("not nice");
        }           
    }   
}

这篇文章很有帮助:https ://github.com/spring-projects/spring-boot/issues/7321

于 2020-10-07T13:03:20.110 回答
-7

在您的控制器中:

throw new Exception("Athlete with same username already exists...");

在您的测试中:

    try {
        mockMvc.perform(post("/api/athlete").contentType(contentType).
                content(TestUtil.convertObjectToJsonBytes(wAthleteFTP)))
                .andExpect(status().isInternalServerError())
                .andExpect(content().string("Athlete with same username already exists..."))
                .andDo(print());
    } catch (Exception e){
        //sink it
    }
于 2018-05-28T10:19:18.603 回答