JUnit单元测试

一. 单元测试步骤:

  1. 引入jar包 junit-*.jar
  2. 创建UnitTestBase类
  3. 创建具体执行单元测试的类,以及类的方法 
  4. 执行时可指定方法执行,也可以执行整个类,此时会依次执行每个方法

 

二. 使用概述

  • UnitTestBase可以作为所有执行类的父类,也就是以后进行单元测试,所有执行类都需要继承UnitTestBase类
  • 在创建的测试类中,构造无参的构造方法,通过super向UnitTestBase传入参数
  • 测试类和测试类中的方法都需要加注解
  • 类:@RunWith(BlockJUnit4ClassRunner.class)
  • 方法:@Test

 

三. 方法1

父类:UnitTestBase.java


import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

public class UnitTestBase {

	private ClassPathXmlApplicationContext context;

	private String springXmlpath;

	public UnitTestBase() {
	}

	public UnitTestBase(String springXmlpath) {
		this.springXmlpath = springXmlpath;
	}

	@Before
	public void before() {
		if (StringUtils.isEmpty(springXmlpath)) {
			springXmlpath = "classpath*:spring-*.xml";
		}
		try {
			context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
			context.start();
		} catch (BeansException e) {
			e.printStackTrace();
		}
	}

	@After
	public void after() {
		context.destroy();
	}

	@SuppressWarnings("unchecked")
	protected <T extends Object> T getBean(String beanId) {
		try {
			return (T) context.getBean(beanId);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}

	protected <T extends Object> T getBean(Class<T> clazz) {
		try {
			return context.getBean(clazz);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}

}

 

测试类:TestBeanLifeCycle.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
 
import test.UnitTestBase;
 
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanLifeCycle extends UnitTestBase{
	public TestBeanLifeCycle(){
		super("classpath*:spring-bean-lifeCycle.xml");
	}
	
	@Test
	public void test(){
		super.getBean("beanLifeCycle");
	}
}
 

说明:

  1. 在执行test方法时,会先执行UnitTestBase的before方法,之后会执行after方法
  2. 每次执行一次test方法都可以看做打开一个IOC容器,通过getbean的方法,从IOC容器中得到一个实例对象
  3. 在测试类的构造方法中传入xml文件的名称(位于src文件夹下)

 

四. 方法2

若不使用单元测试,在Main方法中也是可行的

import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class MainTest {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context;
		
		String springXmlpath="classpath*:spring-bean-lifeCycle.xml";
		
		try {
			context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
			context.start();
			
			context.getBean("beanLifeCycle");
			
			context.destroy();
		} catch (BeansException e) {
			e.printStackTrace();
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值