单元测试的时调用接口对象出现空指针异常,加注解@WebAppConfiguration后可以了

Spring MVC+Junit测试出错:Failed to load ApplicationContext


我是单元测试的时候接口对象调用时出现空指针异常;原因是没有加上注解@WebAppConfiguration

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘serverOperate’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘parkingManager’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}

这里一定要加上

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration(value = "src/main/webapp")

@WebAppConfiguration测试类的存在表明WebApplicationContext应该使用Web应用程序根路径的默认值为测试加载a。要覆盖默认值,请通过该value()属性指定显式资源路径。
一定要注意,@WebAppConfiguration必须与 @ContextConfiguration单个测试类或测试类层次结构一起使用。

你可能感兴趣的:(错误分类)