1.前提spring环境已搭建好
2.导入jar包
这里使用maven工程,非maven工程自行下载导入(JUnit 4 (官方下载:http://www.junit.org/))
<!-- junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
3.编写测试类,代码如下,注解注释在代码中
package com.xunjie.test;
import java.util.Map;
import org.apache.log4j.Logger;
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.alibaba.fastjson.JSON;
import com.xunjie.common.service.FeeBaseService;
//测试运行器,JUnit所有的测试方法都是由测试运行器负责执行。
//这里是spring提供了org.springframework.test.context.junit4.SpringJUnit4ClassRunner作为Junit测试环境
@RunWith(SpringJUnit4ClassRunner.class)
/*
* @ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件 单个文件
*
* @ContextConfiguration(Locations="applicationContext.xml") 多个文件时,可用{}
*
* @ContextConfiguration(locations = { "classpath*:/spring1.xml",
* "classpath*:/spring2.xml" })
*/
@ContextConfiguration(locations = { "classpath:spring.xml" })
public class test {
private static Logger logger = Logger.getLogger(test.class);
@Autowired
private FeeBaseService feeBaseService;
@SuppressWarnings("rawtypes")
/*
* @Test注解的public void方法将会被当做测试用例
*
* JUnit每次都会创建一个新的测试实例,然后调用@Test注解方法
*
* 任何异常的抛出都会认为测试失败
*
* @Test(timeout = xxx) 注解:设置当前测试方法在一定时间内运行完,否则返回错误;
*
* @Test(expected = Exception.class) 注解:设置被测试的方法是否有异常抛出。抛出异常类型为:Exception.class;
*/
@Test
public void test() {
Map user = feeBaseService.getStudent("000001");
System.err.println("-----spring整合JUNIT4进行单元测试-start-----");
System.err.println(JSON.toJSONString(user));
logger.info(JSON.toJSONString(user));
System.err.println("-----spring整合JUNIT4进行单元测试-end-----");
}
}
4.右键方法名,选择则“Run As”→“JUnit Test”执行
5.执行结果
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring.xml]
[org.springframework.context.support.GenericApplicationContext] - Refreshing org.springframework.context.support.GenericApplicationContext@1a108c98: startup date [Thu Mar 01 10:48:07 CST 2018]; root of context hierarchy
[org.springframework.context.support.PropertySourcesPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties]
[org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
-----spring整合JUNIT4进行单元测试-start-----
{"AGE":"20","CLASS_ID":"000001","SEX":"男","STUDENT_ID":"000001","STUDENT_NAME":"张三"}
[com.xunjie.test.test] - {"AGE":"20","CLASS_ID":"000001","SEX":"男","STUDENT_ID":"000001","STUDENT_NAME":"张三"}
-----spring整合JUNIT4进行单元测试-end-----
[org.springframework.context.support.GenericApplicationContext] - Closing org.springframework.context.support.GenericApplicationContext@1a108c98: startup date [Thu Mar 01 10:48:07 CST 2018]; root of context hierarchy