title: MockMVC returns 404 and pageNotFound warning in SpringTest
body: <p>I have been researching for the past week and cannot find a explanation as to why my test case in springMVC is throwing a 404. It logs a warning that the page is not found. I quite certain that my config is correct and the application context is loaded. Why is the mockMvc not performing the request and returning a 200 status, etc.?</p>
  
  <p>Here is my Test class:</p>
  
  <pre><code>    package com.fdmgroup.issuetracker.controller;
  
  import static org.junit.Assert.*;
  
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.test.web.servlet.MockMvcBuilder;
  import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.TestContext;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.context.web.WebAppConfiguration;
  import org.springframework.web.context.WebApplicationContext;
  import org.springframework.web.servlet.view.InternalResourceViewResolver;
  
  import com.fdmgroup.issuetracker.model.impl.User;
  
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("classpath:*/test-context.xml")
  @WebAppConfiguration
  public class LoginControllerTest {
      @Autowired
      private WebApplicationContext wac;
  
      private MockMvc mockMvc;
      private User user;
      @Before
      public void setUp() throws Exception {
  
  
          this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  
      }
  
      @After
      public void tearDown() throws Exception {
      }
  
      @Test
      public void testIfDisplayLoginPageReturnsLoginJsp() throws Exception {
      mockMvc.perform(get("/login")).andExpect(status().isOk());
      }
  
      @Test
      public void testLoginServlet() throws Exception {
      String username = "Corey";
      String password = "Password";
  
          mockMvc.perform(post("/LoginServlet").param("username", username).param("password", password)).andExpect(status().isOk());
      }
  
  }
  </code></pre>
  
  <p>And the trace along with the console logs:</p>
  
  <pre><code>java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;404&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$10.match(StatusResultMatchers.java:655)
      at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
      at com.fdmgroup.issuetracker.controller.LoginControllerTest.testIfDisplayLoginPageReturnsLoginJsp(LoginControllerTest.java:48)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      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:75)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
      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:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
      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)
  
  
  
  12:26:59  INFO [main] - org.springframework.test.context.web.WebTestContextBootstrapper.getDefaultTestExecutionListenerClassNames - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
  12:26:59  INFO [main] - org.springframework.test.context.web.WebTestContextBootstrapper.instantiateListeners - Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
  12:26:59  INFO [main] - org.springframework.test.context.web.WebTestContextBootstrapper.instantiateListeners - Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
  12:26:59  INFO [main] - org.springframework.test.context.web.WebTestContextBootstrapper.getTestExecutionListeners - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@5afa04c, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@6ea12c19, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6a024a67, org.springframework.test.context.support.DirtiesContextTestExecutionListener@7921b0a2]
  12:27:00  INFO [main] - org.springframework.web.context.support.GenericWebApplicationContext.prepareRefresh - Refreshing org.springframework.web.context.support.GenericWebApplicationContext@6a41eaa2: startup date [Thu Jan 11 12:27:00 EST 2018]; root of context hierarchy
  12:27:00  INFO [main] - org.springframework.mock.web.MockServletContext.log - Initializing Spring FrameworkServlet ''
  12:27:00  INFO [main] - org.springframework.test.web.servlet.TestDispatcherServlet.initServletBean - FrameworkServlet '': initialization started
  12:27:00  INFO [main] - org.springframework.test.web.servlet.TestDispatcherServlet.initServletBean - FrameworkServlet '': initialization completed in 162 ms
  12:27:00  WARN [main] - org.springframework.web.servlet.PageNotFound.noHandlerFound - No mapping found for HTTP request with URI [/login] in DispatcherServlet with name ''
  12:27:00  INFO [Thread-0] - org.springframework.web.context.support.GenericWebApplicationContext.doClose - Closing org.springframework.web.context.support.GenericWebApplicationContext@6a41eaa2: startup date [Thu Jan 11 12:27:00 EST 2018]; root of context hierarchy
  </code></pre>
  
  <p>Finally my context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"&gt;
  
      &lt;aop:aspectj-autoproxy proxy-target-class="true" /&gt;
      &lt;context:annotation-config/&gt;
  
      &lt;context:component-scan base-package="com.fdmgroup.issuetracker" /&gt;
      &lt;bean id="UserDAO" class="com.fdmgroup.issuetracker.model.impl.UserDAO"&gt;&lt;/bean&gt;
      &lt;bean id="IssueDAO" class="com.fdmgroup.issuetracker.model.impl.IssueDAO"&gt;&lt;/bean&gt;
      &lt;bean id="newUser" class="com.fdmgroup.issuetracker.model.impl.User"
          scope="prototype"&gt;&lt;/bean&gt;
      &lt;bean id="newIssue" class="com.fdmgroup.issuetracker.model.impl.Issue"
          scope="prototype"&gt;&lt;/bean&gt;
      &lt;bean id="newRole" class="com.fdmgroup.issuetracker.model.impl.Role" scope="prototype"&gt;&lt;/bean&gt;
      &lt;bean id="newDept" class="com.fdmgroup.issuetracker.model.impl.Department" scope="prototype"&gt;&lt;/bean&gt;
      &lt;bean id="newIssueUpdate" class="com.fdmgroup.issuetracker.model.impl.IssueUpdate" scope="prototype"&gt;&lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Let me know if anything else is needed to analyse the issue.</p>