Spring基础之Junit测试

注意这个工程中有3个source folder:src,resources,test。在Eclipse中,它们的图标与普通文件夹是不同的。实际上,这3个文件夹相当于同一个文件夹。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值