junit学习(八)——spring的单元测试

1、在项目中添加Spring的Libraries

在项目上右击---->Properties---->Java Build  Path---->Libraries选项卡---->Add Library---->MyEclipse Libraries---->Spring x.x Core Libraries。我这里添加的是2.5版本。

2、在src下新建Spring的配置文件:applicationContext.xml,并新建一个Date的bean,用于测试。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	
	<bean id="date" class="java.util.Date"/>
</beans>

3、新建单元测试类进行测试

package com.wjl.junit;


import java.util.Date;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Junit_demo_11
 * spring的单元测试
 * **/
public class SpringTest {

	private static ApplicationContext context = null;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	@Test
	public void test(){
		Date date = (Date)context.getBean("date");
		System.out.println(date);//结果:Tue Jan 16 10:03:21 CST 2018,说明spring已经添加到项目中
	}
}

 说明Spring添加成功。

猜你喜欢

转载自1017401036.iteye.com/blog/2407826