一、JUnit单元测试案例
1、pom.xml
Maven工程的pom.xml文件中需要添加JUnit4依赖,例如之前我们的工程中pom.xml有如下描述:
<!-- 添加junit4依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 指定范围,在测试时才会加载 -->
<scope>test</scope>
</dependency>
2、AppTest.java
package com.scy.ssm;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.scy.ssm.service.OpenService;
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
//@ContextConfiguration({"classpath:spring-mybatis.xml"}) //加载配置文件
//使用通配符时,classpath后要加入*,否则通配符不能生效
@ContextConfiguration({"classpath*:spring-my*.xml"})
public class AppTest {
Logger logger = LogManager.getLogger(AppTest.class);
@Resource
OpenService openService;
@Test
public void transactionTest() {
System.out.println("funfindData");
List<Map<String, Object>> list = openService.findDept();
}
}
二、@ContextConfiguration
@ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件
1.1 单个文件
@ContextConfiguration(locations="../applicationContext.xml")
@ContextConfiguration(classes = SimpleConfiguration.class)
1.2 多个文件
(1)数组
可用{}
@ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })
(2)通配符
使用通配符时,classpath后要加入*,否则通配符不能生效
@ContextConfiguration({"classpath*:spring-my*.xml"})
1.3 默认不写
可以根据测试的类名,去找到与之对应的配置文件。
异常情况:
异常信息
四月 12, 2017 9:38:10 上午 org.springframework.test.context.support.AbstractContextLoader generateDefaultLocations
信息: Could not detect default resource locations for test class [soundsystem.CNamespaceValueTest]: class path resource [soundsystem/CNamespaceValueTest-context.xml] does not exist
四月 12, 2017 9:38:10 上午 org.springframework.test.context.support.AnnotationConfigContextLoaderUtils detectDefaultConfigurationClasses
信息: Could not detect default configuration classes for test class [soundsystem.CNamespaceValueTest]: CNamespaceValueTest does not declare any static, non-private, non-final, inner classes annotated with @Configuration.
三、常见问题
1、无法找到映射的sql
(1)报错
使用 JUnit
测试时,报错没有找到相应的 sql
。报错信息如下:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for primarySchoolScore.findStudentScoreSumScore
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for primarySchoolScore.findStudentScoreSumScore
(2)原因分析
原因可能是 spring-mybatis.xml
这个配置文件中,对mapperLocations
的配置中加入了 *
通配符。
(3)解决问题-方法一
对 spring-mybatis.xml
这个配置文件进行修改。
原配置
<property name="mapperLocations" value="classpath:com/scy/*/mapping/**/*.xml"/>
修改后的配置(将*替换为ssm)
<property name="mapperLocations" value="classpath:com/scy/ssm/mapping/**/*.xml"/>
(4)解决问题-方法二
对项目中的 spring-mybatis.xml
配置文件进行修改,在classpath
后面多加一个*
;这样的意思是可以读取到jar包中 sql
的 xml
配置文件。
事实上我的 sql
的 xml
配置文件也没有放到jar包中,但是经过这样的修改,就不再报错。
原配置
<property name="mapperLocations" value="classpath:com/scy/*/mapping/**/*.xml"/>
修改后的配置(在classpath
后加入*
)
<property name="mapperLocations" value="classpath*:com/scy/*/mapping/**/*.xml"/>
classpath和classpath*的区别
- classpath:只会到你的class路径中查找找文件。
- classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。
注意: 用classpath*:需要遍历所有的classpath,所以加载速度是很慢的;因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*。