'How can MockMvc get around API exceptions?

Ideally, I want this:

    @Test
    void testWelcome() throws Exception {
        mockMvc.perform(get("/oups"))
            .andExpect(status().isInternalServerError());
    }

But the test fails because it throws at get("/oops") (which runs a controller method that throws a RuntimeException) before it can get to the assertion. How do I deal with this issue? This is my quick fix for now:

    @Test
    void testTriggerException() throws Exception {
        try {
            mockMvc.perform(get("/oops"))
            .andExpect(status().isInternalServerError());
        } catch (Exception e) {
            return;
        }
        fail();
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source