Jaap Oudejans
Jaap Oudejans

Reputation: 718

Spring-mvc + MockMvc + junit result is 200 but should be 404

I use the spring-test framework version 3.2.1.RELEASE and, specifically, the MockMvc object to test my controllers.

The problem, however, is that when the MockMvc object does a perform on a non-existing html page, it returns status 200, but my browser displays a 404 status - which is correct, because the page does not exists. How can I check for the 404 status? (since that is the status I expect)

@Test
public void testInvalidUrl() throws Exception {
    mockMvc.perform(get("/invalidJSPFileName.html"))
            .andExpect(status().isNotFound())
            .andExpect(forwardedUrl("/WEB-INF/jsp/invalidJSPFileName.jsp"));
}

Controller of the test case:

@Controller
@RequestMapping("/")
public class IndexController {
    @RequestMapping("*.html")
    public void showIndex(){

    }
}

Resolver:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

Directory:

WEB-INF
    jsp
        index.jsp

Upvotes: 1

Views: 2047

Answers (1)

Stephane
Stephane

Reputation: 12740

Your controller accepts anything with the suffix .html

@RequestMapping("*.html")

So, your request should be found.

Upvotes: 1

Related Questions