---
title: "junit实现测试类（在Spring2.5中）"
author: "Jatula"
date: "2009-01-17T15:14:54Z"
tags: []
categories: ["企业架构"]
slug: "junit-spring2-5"
draft: false
lastmod: "2009-01-17T15:16:36Z"
id: 317888
url: "https://jatula.iteye.com/blog/317888"
---

第一步：

创建JAVA项目，也可以是Web项目，加入Spring2.5的Core Lib

 

创建注入类（测试用）

 

修改applicationContext.xml文件

 

内容如下：

 

注入类：

```
package nell;

public class Animal {
	private String one;
	private String twe;

	public String getOne() {
		return one;
	}

	public void setOne(String one) {
		this.one = one;
	}

	public String getTwe() {
		return twe;
	}

	public void setTwe(String twe) {
		this.twe = twe;
	}
}
```

 

applicationContext.xml文件

 

```
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="animal" class="nell.Animal">
	<property name="one" value="123"/>
	<property name="twe" value="小到堆"/>
</bean>

</beans>
```

 

测试类

 

```
package nell;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestE {
	@Test
	public void haha(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Animal aa=(Animal)ac.getBean("animal");
		assertEquals("123",aa.getOne());
		assertEquals("小到堆",aa.getTwe());
	}
}
```

 

注意点：

@Test

在上面打入@Test，如果出现报错，右键加入Jar包，在MyEclipse Lib下面有个Spring2.5 Testing Support Lib包

因为Spring已集成

好了，把Junit运用了，assertEquals("123",aa.getOne());做了比较；