Spring基础之Junit测试
2013-04-28 02:31
411 查看
注意这个工程中有3个source folder:src,resources,test。在Eclipse中,它们的图标与普通文件夹是不同的。实际上,这3个文件夹相当于同一个文件夹。
application-ctx.xml
给Eclipse工程中添加Junit库:
运行程序:
源代码,原文:
http://pan.baidu.com/share/link?shareid=423718&uk=3878681452 http://developers-blog.org/blog/d/Java/2010/01/11/Spring-Framework-Example
application-ctx.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.xsd"> <!-- Initialization of DataSource object. Add some customers to data member --> <bean id="dataSource" class="org.developers.blog.spring.example.DataSource"> <property name="data"> <map> <entry key="0"> <bean class="org.developers.blog.spring.example.Customer"> <property name="id" value="0"/> <property name="name" value="Rafael Sobek"/> <property name="address" value="Nirvana 1"/> </bean> </entry> <entry key="1"> <bean class="org.developers.blog.spring.example.Customer"> <property name="id" value="1"/> <property name="name" value="宅未央"/> <property name="address" value="Nirvana 2"/> </bean> </entry> </map> </property> </bean> <!-- references DataSource object and lists the customer --> <bean id="customerManager" class="org.developers.blog.spring.example.CustomerManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
package org.developers.blog.spring.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.junit.Test; public class SpringExampleTest { @Test public void testInstrumentalisation() { ApplicationContext appCtx = new ClassPathXmlApplicationContext("application-ctx.xml"); CustomerManager customerManager = (CustomerManager) appCtx.getBean("customerManager"); customerManager.listCustomersOnConsole(); } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.developers.blog.spring.example; import java.util.Collection; import java.util.Hashtable; import java.util.Map; /** * * @author rafsob */ public class DataSource { public Map<Integer, Customer> getData() { return data; } public void setData(Map<Integer, Customer> data) { this.data = data; } private Map<Integer, Customer> data; int i = 0; public void addCustomer(Customer customer) { customer.setId(i++); data.put(customer.getId(), customer); } public Collection<Customer> getCustomers() { return data.values(); } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.developers.blog.spring.example; /** * * @author rafsob */ public class CustomerManager { private DataSource dataSource; public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void listCustomersOnConsole() { for (Customer customer:this.dataSource.getCustomers()) { System.out.println("Customer: " + customer); } } }
给Eclipse工程中添加Junit库:
运行程序:
源代码,原文:
http://pan.baidu.com/share/link?shareid=423718&uk=3878681452 http://developers-blog.org/blog/d/Java/2010/01/11/Spring-Framework-Example
相关文章推荐
- junit基础学习之-引用spring容器的测试(7)
- 利用spring进行简化测试(JUNIT)
- android基础--eclipse Junit的功能测试
- JUNIT测试Hibernate和Spring的Dao层方法出现的问题
- springmvc controller junit 测试
- junit4.9测试用例 spring测试用例 Assert 注解
- 在hibernate和spring项目用junit测试总是出现java.lang.Boolean cannot be cast to java.lang.Integer
- junit测试spring整合activeMQ报错:java.lang.IllegalStateException: Failed to load ApplicationContext
- Spring笔记(第三弹:使用JUnit对Spring工程进行测试)
- JavaEE_Junit_Junit对Spring注入的Service层的测试
- spring junit完美测试解决方案
- Junit测试一个spring静态工厂实例化bean的例子,出现java.lang.IllegalArgumentException异常。
- spring下的junit测试
- spring框架学习(三)spring与junit整合测试
- 基于Spring的可复用的Junit测试类的设计
- spring测试junit事务管理及spring面向接口注入和实现类单独注入(无实现接口),实现类实现接口而实现类单独注入否则会报错。
- 搭建项目Maven+springMVC+hibernate时,JUnit测试出现报ClassNotFoundException错误的解决
- Junit+spring创建JNDI运行测试用例
- 用JUnit测试Spring中的配置,需要使用ApplicationContext
- Hibernate4与Spring4整合,使用Junit4测试相关学习笔记