Sping MVC 整合Junit4進行單元測試及常見錯誤解決
(1)maven依賴:添加spring-test和Junit4 jar包
對於jdk1.7版本,spring4版本,依賴如下:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.9.RELEASE</version> </dependency>
對於jdk1.8,spring 5,依賴如下:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.9.RELEASE</version> </dependency>
註意:這裏的junit和spring-test版本號,否則報錯;junit和spring-test之間具有版本對應關系(上面的對應關系是經過多次測試得到的)。
(2)編寫單元測試:
package com.shang; import java.util.List; 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.SpringJUnit4Cla***unner; import com.alibaba.fastjson.JSON; import com.shang.pojo.Account; import com.shang.service.AccountService; @RunWith(SpringJUnit4Cla***unner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class TestController { @Autowired AccountService accountService; @Test public void test(){ List<Account> list=accountService.getAll(); System.out.println(JSON.toJSONString(list)); } }
註意:(1)ContextConfiguration(locations={"classpath:applicationContext.xml"}) 中的applicationContext.xml的存儲路徑為:src/main/resources
(2)ContextConfiguration(locations={"classpath:applicationContext.xml","其他路徑"})中的locations中可以指定多個路徑。
2.單元測試中的註釋含義:
(1)@RunWith 註釋是 Junit 提供的,用來說明此測試類的運行者,這裏用了 SpringJUnit4Cla***unner,這個類是一個針對 Junit 運行環境的自定義擴展。
(2)@ContextConfiguration 註釋是 Spring test context 提供的,用來指定 Spring 配置信息的來源,支持指定 XML 文件位置或者 Spring 配置類名,這裏我們指定 classpath 下的 applicationContext.xml為配置文件的位置。
(3)@Autowired 進行對象的實例化,用來說明測試類是在 Spring 容器中管理的,可以獲取容器的 bean進行註入,不用手工獲取要測試的 bean 實例了。
3.完整的Spring MVC和單元測試的pom文件如下:
<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>com.shangh</groupId>
<artifactId>SSM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<!-- 資源拷貝插件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!--控制tomcat端口號 -->
<configuration>
<port>8080</port>
<!-- 發布到tomcat後的名稱 -->
<!--/ 相當於把項目發布成ROOT -->
<path>/abc</path>
<uriEncoding>UTF-8</uriEncoding>
<!-- <finalName>mgr</finalName>
<server>tomcat7</server> -->
<username>tomcat</username>
<password>tomcat </password>
<url>http://localhost:8080/manager/text</url>
</configuration>
</plugin>
</plugins>
</build>
</project>
通過以上配置,就可以在spring中進行單元測試了。
4.單元測試配置時的錯誤
(1)org.springframework.test.context.junit4.SpringJUnit4Cla***unner的解決
原因:依賴的jar包對應的版本不一致。
解決:(1)添加依賴:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
(2)右鍵根項目—maven—update dependencies.重新更新依賴關系,讓工程可以找到最新的依賴。
Sping MVC 整合Junit4進行單元測試及常見錯誤解決