摘要:
记录一下自己用过的单元测试方法
-1 注意
- 加了@test的类 不能有返回值和参数
0 中间遇到的bug
中间遇到一个maven插件的问题
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-cli) on project spring_junit_module: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
可能的解决办法 用法二
https://blog.csdn.net/u011781521/article/details/74868680
其实后来发现,ide下面的event log里面也有报错,重启了一次ide
1.法一 用IntelliJ
优点:简单
缺点:不能批量处理吧
可参考:
https://blog.csdn.net/yangshijin1988/article/details/63262400
2 法二 使用spring junit
优点:直接加注释就行了
缺点:配置有点麻烦
package test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:dispatcher-servlet.xml"}) //加载配置文件
@RunWith(SpringJUnit4ClassRunner.class)
public class BaseJunit4Test {
}
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:dispatcher-servlet.xml"}) //加载配置文件
@RunWith(SpringJUnit4ClassRunner.class)
public class test extends BaseJunit4Test{
@Test
public void test (){
System.out.print("ok");
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--其实要不要都无所谓-->
<context:annotation-config/>
<context:component-scan base-package="test" />
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>spring_junit_module</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version> 3.2.4.RELEASE </version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<!--这是一个插件 不加可能会报错-->
<!--其实要不要都无所谓-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的具体操作:
1.新建一个springmvc项目
2.添加maven支持
3.修改application,xml 并把这两个文件放到resource目录下
4.增加和修改上面的文件
5.运行 :直接点击方法左边的运行按钮就行 无需启动整个项目
项目源码:
项目1:
https://github.com/sunny73/WEB_Projects/commit/11438da6256fce670816fe33c82c314e0b69dffc
项目2:
https://github.com/sunny73/WEB_Projects/commit/7ab17ff76c072bf4a37a2bd74857864e7b6ee1d0