Spring有一个测试框架,可以整合JUnit进行测试,既简单有方便。
加入依赖
JUnit官方链接有几个可用的版本点击打开链接
Spring test以及其他依赖如下
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>my</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>my Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- Test start -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Test end -->
<!-- XML -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.6-2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.5</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- Json -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.3</version>
</dependency>
<!-- Util -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<build>
<finalName>my</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>
</project>
创建测试目录及类
maven中默认的都是src/test/java,这里src/test/java/config下放spring测试配置文件。
为了方便,测试类这里创建了两个AbstractTest.java,这个类主要完成测试前后需要准备的环境等。
package my;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
public class AbstractTest {
@Before
public void setUp() {
try {
System.out.println("@Before");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@BeforeClass
public static void before() throws Exception {
try {
System.out.println("@BeforeClass");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@After
public void tearDown() {
System.out.println("@After");
}
@AfterClass
public static void remove() throws Exception {
try {
System.out.println("@AfterClass");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
当然还有我们自己的测试类了。该类继承刚才的类。
package my;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.my.web.service.SayHiService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/java/config/dispatcher-servlet.xml" })
public class MyTest extends AbstractTest {
@Autowired
private SayHiService service;
@Test
public void testCase() {
String config = "MyTestCase";
System.out.println("@Testing-----");
System.out.println(service.SayHiDefault("aaaaa"));
Assert.assertEquals(config, config);
}
}
这里注意类之前的配置,用于加载测试的配置文件。
结束
到此,我们的测试就完成了,当然Spring的配置不止这些,例如测试中事务的配置,环境的切换等。有兴趣自己可以尝试。
另外,通过我们的测试输出可以看出JUnit内置的注解的执行顺序。
@Before
@Testing-----
Hi, Johness!
@After
@AfterClass