gpt4 book ai didi

java - Spring MVC 测试 : java. lang.AssertionError: 找不到 ModelAndView

转载 作者:行者123 更新时间:2023-11-28 21:27:12 26 4
gpt4 key购买 nike

我是所有这些东西的新手,为 Spring MVC Controller 编写测试并遇到上面的错误,尝试了提供的解决方案 Spring 3.2.5 error "java.lang.AssertionError: No ModelAndView found" for all my controller Unit Test - 仍然没有运气!有人可以帮忙吗?提前致谢!

如果下面的代码还需要其他任何东西,请告诉我。

测试类:

(注意,getBeanDefinitionNames() 等的打印输出返回“homeController”——所以我假设 Controller 已找到并已初始化)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={WebDispatcherConfig.class,DataConfig.class})
@WebAppConfiguration
public class HomeControllerTest {

private MockMvc mockMvc;

@Autowired
private WebApplicationContext wac ;

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

}

@Test
public void testHome() throws Exception {


mockMvc
.perform(get(WebDispatcherConfig.WEBAPP_PREFIX))
.andExpect(view().name(HomeController.HOME))
.andExpect(model().attributeExists(WebDispatcherConfig.PREFIX_NAME));
}

}

网络上下文配置类:

@EnableWebMvc
@ComponentScan("io.github.d2edev.mywebapp.web")
public class WebDispatcherConfig extends WebMvcConfigurerAdapter {
public static final String WEBAPP_PREFIX="/web";
public static final String PREFIX_NAME="wepbrefix";

@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}



@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

Controller (注意它已经在上面的配置类中的@ComponentScan 中反射(reflect)的包中):

package io.github.d2edev.mywebapp.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import io.github.d2edev.mywebapp.config.WebDispatcherConfig;


@Controller
@RequestMapping("/")
public class HomeController {
public static final String HOME="home";

@RequestMapping(method=RequestMethod.GET)
public String home(Model model){
model.addAttribute(WebDispatcherConfig.PREFIX_NAME, WebDispatcherConfig.WEBAPP_PREFIX);
return HOME;
}

堆栈跟踪:

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:68)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
at test.web.HomeControllerTest.testHome(HomeControllerTest.java:52)
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:498)
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.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)

最佳答案

解决了!原因是在我的真实应用程序中,servlet 被映射到“/web”,HomeController 被映射到“/”,这导致 Controller 为“/web”的请求提供服务。所以我在 Controller 测试类中为 GET 请求提供了相同的“/web”地址,但是当我们创建 MockMvc 时,据我所知,我们不能像在 app init 类中那样进行映射,所以“测试” Controller 正在等待我在“/”请求。为了让它工作,我应该明确地把

.perform(get("/"))

或使用此处建议的附加代码进行即时重新映射:

MockMvc WebAppConfiguration: Load servlet mappings in web.xml

谢谢!

关于java - Spring MVC 测试 : java. lang.AssertionError: 找不到 ModelAndView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37055687/

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