spring整合JUNIT4进行单元测试

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
  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

低调D树苗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值