gpt4 book ai didi

java - 我的所有 Controller 单元测试的 Spring 3.2.5 错误 "java.lang.AssertionError: No ModelAndView found"

转载 作者:行者123 更新时间:2023-11-30 08:27:54 24 4
gpt4 key购买 nike

我正在尝试在 Spring MVC 中为我的 Controller 编写一些单元测试,但我的单元测试已经出现错误:“java.lang.AssertionError:找不到 ModelAndView。

我的应用程序和 Controller 工作正常。

有没有人有什么想法?

代码 Controller :

@Controller
public class LoginController {
@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
public String loginerror(ModelMap model) {
model.addAttribute("error", "true");
return "accueilLogin";

}
}

单元测试:

ContextConfiguration(locations = { "classpath:/META-INF/applicationContext-test.xml" })
@WebAppConfiguration
public class LoginControllerTest extends AbstractJUnit4SpringContextTests {

@Autowired
private WebApplicationContext wac ;

private MockMvc mockMvc;

@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.wac).build();
System.out.println(wac.getBeanDefinitionNames());

}
@Test
public void testLoginFailed() throws Exception {
this.mockMvc.perform(get("/loginfailed")).andDo(print())
.andExpect(view().name("accueilLogin"));
}

Spring 安全配置:

    <intercept-url pattern="/accueilLogin*" access="ROLE_ANONYMOUS, ROLE_USER" />
<intercept-url pattern="/register*" access="ROLE_ANONYMOUS, ROLE_USER" />
<intercept-url pattern="/welcome*" access="ROLE_USER" />
<!-- <intercept-url pattern="/logout*" access="ROLE_USER" /> -->
<form-login login-page="/accueilLogin" default-target-url="/welcome"
authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
</http>

web.xml 配置:

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml,
classpath:/META-INF/applicationContext.xml
</param-value>
</context-param>

<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

请求日志:

MockHttpServletRequest:
HTTP Method = GET
Request URI = /loginfailed
Parameters = {}
Headers = {}

Handler:
Type = null
Resolved Exception:
Type = null

ModelAndView:
View name = null
View = null
Model = null

FlashMap:

MockHttpServletResponse:
Status = 404
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []

堆栈跟踪:

java.lang.AssertionError: No ModelAndView found
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:39)
at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:72)
at org.springframework.test.web.servlet.result.ViewResultMatchers$2.match(ViewResultMatchers.java:62)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:148)
at fr.web.LoginControllerTest.testLoginFailed(LoginControllerTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
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:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
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:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

最佳答案

您的日志显示您收到 404

Status = 404

这意味着未找到您的处理程序。

我愿意打赌你的@Controller没有被注册。你已经像这样加载了你的配置

@ContextConfiguration(locations = { "classpath:/META-INF/applicationContext-test.xml" })

是否 applicationContext-test.xml做一个<component-scan>在您的 Controller 所在的包装上?它做 <mvc:annotation-driven> 吗? ?如果没有,则您的应用程序尚未注册您的处理程序方法。

关于java - 我的所有 Controller 单元测试的 Spring 3.2.5 错误 "java.lang.AssertionError: No ModelAndView found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20547922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com