How to unit test when spring fails to resolve a view
<p>I have a spring unit test which is showing success even though if the same request is made in the web browser it returns 404 as the view is not found.</p>

<p>This is a deliberate test because i would like to catch errors in unit test if controller is returning a view which doesnt exist. </p>

<p>However in the unit test, i have set to look for 404 but http response code is 200. How can i adapt my application or test case to catch errors where a view is returned from the controller which doesnt exist.</p>

<p><strong>Controller</strong></p>

<pre><code>package com.doyleisgod.springmavenexample.controllers;


import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller 
public class HomeController {
    Logger log = Logger.getLogger(this.getClass());

    @RequestMapping("/testings.htm")
    public String Index (Model model){
        log.debug("creating the testing page");
        model.addAttribute("name", "Chris");
        return "something";
    }
}
</code></pre>

<p><strong>Test Class</strong></p>

<pre><code>package com.doyleisgod.springmavenexample.controllers.tests;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:testContext.xml", "classpath:smeApplicationContext-web.xml"})
@WebAppConfiguration
public class HomeControllerTest {
       private MockMvc mockMvc;

        @Autowired
        private WebApplicationContext webApplicationContext;

        @Before
        public void setUp() {
            //We have to reset our mock between tests because the mock objects
            //are managed by the Spring container. If we would not reset them,
            //stubbing and verified behavior would "leak" from one test to another.
            mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        }

       @Test
        public void indexTest() throws Exception {


            mockMvc.perform(get("/testings.htm", 1L))
                    .andExpect(status().isNotFound());
        }
}
</code></pre>

<p><strong>Stack Trace</strong></p>

<pre><code>java.lang.AssertionError: Status expected:&lt;404&gt; but was:&lt;200&gt;
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
    at org.springframework.test.web.servlet.result.StatusResultMatchers$5.match(StatusResultMatchers.java:556)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:149)
    at com.doyleisgod.springmavenexample.controllers.tests.HomeControllerTest.indexTest(HomeControllerTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
</code></pre>

<p><strong>Console log</strong></p>

<pre><code>6-02-14 20:29:24 DEBUG TestDispatcherServlet:558 - Published WebApplicationContext of servlet '' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.]
2016-02-14 20:29:24 INFO  TestDispatcherServlet:498 - FrameworkServlet '': initialization completed in 21 ms
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:139 - Servlet '' configured successfully
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:843 - DispatcherServlet with name '' processing GET request for [/testings.htm]
2016-02-14 20:29:24 DEBUG RequestMappingHandlerMapping:222 - Looking up handler method for path /testings.htm
2016-02-14 20:29:24 DEBUG RequestMappingHandlerMapping:232 - Did not find handler method for [/testings.htm]
2016-02-14 20:29:24 DEBUG SimpleUrlHandlerMapping:169 - Matching patterns for request [/testings.htm] are [/**]
2016-02-14 20:29:24 DEBUG SimpleUrlHandlerMapping:194 - URI Template variables for request [/testings.htm] are {}
2016-02-14 20:29:24 DEBUG SimpleUrlHandlerMapping:124 - Mapping [/testings.htm] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@18d68ff] and 1 interceptor
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:932 - Last-Modified value for [/testings.htm] is: -1
2016-02-14 20:29:24 DEBUG MockRequestDispatcher:67 - MockRequestDispatcher: forwarding to [default]
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:1019 - Null ModelAndView returned to DispatcherServlet with name '': assuming HandlerAdapter completed request handling
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:991 - Successfully completed request
2016-02-14 20:29:24 DEBUG DirtiesContextTestExecutionListener:94 - After test method: context [DefaultTestContext@1799640 testClass = HomeControllerTest, testInstance = com.doyleisgod.springmavenexample.controllers.tests.HomeControllerTest@b107fd, testMethod = indexTest@HomeControllerTest, testException = java.lang.AssertionError: Status expected:&lt;404&gt; but was:&lt;200&gt;, mergedContextConfiguration = [WebMergedContextConfiguration@163425a testClass = HomeControllerTest, locations = '{classpath:testContext.xml, classpath:smeApplicationContext-web.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], class dirties context [false], class mode [null], method dirties context [false].
2016-02-14 20:29:24 DEBUG ServletTestExecutionListener:134 - Resetting RequestContextHolder for test context [DefaultTestContext@1799640 testClass = HomeControllerTest, testInstance = com.doyleisgod.springmavenexample.controllers.tests.HomeControllerTest@b107fd, testMethod = indexTest@HomeControllerTest, testException = java.lang.AssertionError: Status expected:&lt;404&gt; but was:&lt;200&gt;, mergedContextConfiguration = [WebMergedContextConfiguration@163425a testClass = HomeControllerTest, locations = '{classpath:testContext.xml, classpath:smeApplicationContext-web.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]].
2016-02-14 20:29:24 DEBUG DirtiesContextTestExecutionListener:126 - After test class: context [DefaultTestContext@1799640 testClass = HomeControllerTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@163425a testClass = HomeControllerTest, locations = '{classpath:testContext.xml, classpath:smeApplicationContext-web.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], dirtiesContext [false].
2016-02-14 20:29:24 INFO  GenericWebApplicationContext:873 - Closing org.springframework.web.context.support.GenericWebApplicationContext@124409e: startup date [Sun Feb 14 20:29:23 GMT 2016]; root of context hierarchy
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:249 - Returning cached instance of singleton bean 'lifecycleProcessor'
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:474 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@19fcf7f: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,mvcUriComponentsContributor,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#1,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,viewResolver,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)#18': [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0]
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)#9': [mvcUriComponentsContributor]
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)#10': [(inner bean)#9]
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)#1': [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0]
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)': [org.springframework.web.servlet.handler.MappedInterceptor#0]
</code></pre>