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添加成功。
本文介绍了如何在项目中集成Spring框架并进行基本的单元测试。主要包括:添加Spring库、创建Spring配置文件applicationContext.xml、定义Date类型的bean及通过JUnit进行测试验证。
——spring的单元测试&spm=1001.2101.3001.5002&articleId=84914975&d=1&t=3&u=5ccbf0b7a3df45e288fe691889fc37e9)
7730

被折叠的 条评论
为什么被折叠?



