过去一周我一直在研究,无法找到解释为什么我的springMVC中的测试用例会抛出404.它会记录一个警告,指出找不到页面。我非常确定我的配置是正确的并且加载了应用程序上下文。为什么mockMvc没有执行请求并返回200状态等?
这是我的Test类:
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());
}
}
跟踪控制台的跟踪记录:
java.lang.AssertionError: Status expected:<200> but was:<404>
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
最后我的context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:annotation-config/>
<context:component-scan base-package="com.fdmgroup.issuetracker" />
<bean id="UserDAO" class="com.fdmgroup.issuetracker.model.impl.UserDAO"></bean>
<bean id="IssueDAO" class="com.fdmgroup.issuetracker.model.impl.IssueDAO"></bean>
<bean id="newUser" class="com.fdmgroup.issuetracker.model.impl.User"
scope="prototype"></bean>
<bean id="newIssue" class="com.fdmgroup.issuetracker.model.impl.Issue"
scope="prototype"></bean>
<bean id="newRole" class="com.fdmgroup.issuetracker.model.impl.Role" scope="prototype"></bean>
<bean id="newDept" class="com.fdmgroup.issuetracker.model.impl.Department" scope="prototype"></bean>
<bean id="newIssueUpdate" class="com.fdmgroup.issuetracker.model.impl.IssueUpdate" scope="prototype"></bean>
</beans>
如果需要其他任何东西来分析问题,请告诉我。
答案 0 :(得分:1)
尝试在测试类上方添加此注释
@ContextConfiguration(classes = <YourController>.class)
public class ControllerTest {
答案 1 :(得分:0)
答案在于你所包含的堆栈跟踪
12:27:00 WARN [main] - org.springframework.web.servlet.PageNotFound.noHandlerFound - No mapping found for HTTP request with URI [/login] in DispatcherServlet with name ''
因此,您没有注册处理/login
请求路径的控制器。
您需要包含Spring MVC的XML配置文件才能使测试正常运行。