Spring Web 项目Junit测试报错问题

测试对象是Web项目的Service类,参照网上查到的资料,按如下方式执行时报错,

//使用junit4进行单元测试
@RunWith(SpringJUnit4ClassRunner.class)
//加载配置文件,可以指定多个配置文件,locations指定的是一个数组
@ContextConfiguration(locations={"classpath:spring/applicationContext-*.xml", "classpath:spring/springmvc.xml"})
//启动事务控制
@Transactional
//配置事务管理器,同时指定自动回滚
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
public class BaseJunit4Test {
    //进行测试时,将测试类继承该类
    //注入service对象
    //然后在方法上使用@Test,@RollBack,@Transaction等注解单独修饰
}

执行后报错如下:

  Caused by java.lang.IllegalStateException:WebApplicationObjectSupport instance[ResourceHttpRequestHandler [locations=[class path resource [assert/]],resolvers=[[email protected].]]] does not run in a WebApplicationContext but in : org.springframework.context.support.GenericAppliactionContext............

网上找到了类似的问题,说是在配置文件中将assert相关的静态资源目录去掉就可以了。感觉不靠谱,因为工程在tomcat中启动是没有什么问题的。

刚开始用Junit,不太熟悉,后来想,可能是Junit配置没有支持Web工程。然后又搜索怎么对Controller层进行单元测试的,结果发现了测试类上面的@WebAppConfiguration注解。猜想正是因为少了这个注解的问题。

于是在测试代码中加上了这个注解,就不报错了。

Junit很强大,还是要好好学习一下的。

遇到问题,在此Mark一下。这是我的错题集。

原文地址:https://www.cnblogs.com/aligege/p/8630555.html

时间: 2024-08-11 14:30:00

Spring Web 项目Junit测试报错问题的相关文章

Spring项目JUnit测试报错ClassNotFoundException解决、

MyEclipse项目上有红色感叹号,各包显示正常.用JUnit测试部分能运行,部分报错,报错如下: Class not found n_jdbc.UserDaoTestjava.lang.ClassNotFoundException: n_jdbc.UserDaoTest at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native

maven web 项目中启动报错java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener

本篇文章主要介绍了"maven web 项目中启动报错java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener ",主要涉及到maven web 项目中启动报错java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener 方面的内容,对于maven web 项目中启动报错jav

Eclipse下Maven新建Web项目index.jsp报错完美解决(war包)

Eclipse下Maven新建Web项目步骤 1. 2. 3. 4. 5. 问题描述 最近用eclipse新建了一个maven项目,结果刚新建完成index.jsp页面就报错了,先把错误信息贴出来看看! 解决方法 第一种:直接在pom.xml文件中添加jar包支持 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <

maven 构建web项目index.jsp报错

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path... 解决办法: JSP页面顶端出现“红色”的报错信息:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path.原来Javaweb工程类中没有添加Tomcat运行时相关类导致. 下面是具

web项目启动,报错1099端口被占用的解决办法

问题原因:非正常关闭/终止服务,导致进程仍在继续,当再次启动服务时报错端口被占用 解决办法: 1.打开windows命令窗口,组合键win+R; 2.使用命令netstat -aon |findstr "1099",找出进程对应的PID,如下图 3.杀死进程taskkill -f -pid "8900",结果如下图 到此已完美解决端口占用问题,重启项目即可.

Spring Boot项目Maven Build报错的解决方法

问题1, [ERROR]Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test (default-test) on project motherBuyBoot: There are test failures. [ERROR] Please refer to D:\web\motherbuy\target\surefire-reports for the individual test r

spring cloud gateway网关启动报错:No qualifying bean of type &#39;org.springframework.web.reactive.DispatcherHandler&#39;

网关配置好后启动报错如下: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletEndpoint

Spring Boot 2.1.7 启动项目失败,报错: &quot;Failed to configure a DataSource: &#39;url&#39; attribute is not specified and no embedded datasource could be configured.&quot;

一开始按照网上的很多解决办法是: 启动类头部声明@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}),但是这样会排除掉我自己的数据库配置, 检查了很多地方都没发现问题在哪里,最后自己猜想不论怎么修改application.yml文件,都不生效,是不是因为这个配置文件就没有被加载到? 带着这个想法我就去网上搜了一下问题,有个问题看到了target目录的作用,就去想了一下是不是application.yml文件没有被

ASP.NET MVC 复制MVC项目代码到同一个项目的时候报错The request for ‘home’ has found the following matching controll

ASP.NET MVC 复制MVC项目代码到同一个项目的时候报错The request for 'home' has found the following matching controll "/"应用程序中的服务器错误. Multiple types were found that match the controller named 'home'. This can happen if the route that services this request ('{control