JUnit 测试 Spring

// applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    
<bean id="addressDAO"
        class
="org.kyle.address.dao.impl.AddressDAO">
        
<property name="sessionFactory">
            
<ref local="sessionFactory" />
        
</property>
    
</bean>
    
    
<bean id="dataSource"
        class
="org.apache.commons.dbcp.BasicDataSource">
        
<property name="driverClassName"
            value
="com.mysql.jdbc.Driver">
        
</property>
        
<property name="url"
            value
="jdbc:mysql://localhost:3306/address_book">
        
</property>
        
<property name="username" value="root"></property>
        
<property name="password" value="root"></property>
    
</bean>
    
<bean id="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
<property name="dataSource">
            
<ref bean="dataSource" />
        
</property>
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                
</prop>
            
</props>
        
</property>
        
<property name="mappingResources">
            
<list>
                
<value>
                    org/kyle/address/hibernate/beans/Address.hbm.xml
                
</value>
            
</list>
        
</property>
    
</bean>
</beans>

//IAddressDAO.java

package org.kyle.address.dao;

import java.util.List;

import org.kyle.address.hibernate.beans.Address;

public interface IAddressDAO {
    
//按用户查找所用联系人
    public List findAllByUsername(final String username);
    
//按id 查找联系人
    public Address findById(String id);
    
//判断某个联系人是否存在
    public boolean isExist(final String username,final String name);
    
//插入联系人
    public void insert(Address address);
    
//更新联系人
    public void update(Address address);
    
//删除联系人
    public void delete(String id);
}

//AddressDAO.java

 

 

package org.kyle.address.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.kyle.address.dao.IAddressDAO;
import org.kyle.address.hibernate.beans.Address;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class AddressDAO extends HibernateDaoSupport implements IAddressDAO {

    
public void delete(String id) {
        Object p 
= getHibernateTemplate().load(Address.classnew Integer(id));
        getHibernateTemplate().delete(p);
    }


    
public List findAllByUsername(final String username) {
        
// 按用户名
        return (List) getHibernateTemplate().execute(new HibernateCallback() {
            
public Object doInHibernate(Session session)
                    
throws HibernateException {
                List result 
= session.createCriteria(Address.class).add(
                        Restrictions.eq(
"username", username)).list();
                
return result;
            }


        }
);
    }


    
// 按ID查找联系人
    public Address findById(String id) {

        
return (Address) getHibernateTemplate().get(Address.class,
                
new Integer(id));
    }


    
// 插入联系人
    public void insert(Address address) {
        getHibernateTemplate().save(address);

    }


    
// 判断联系人是否存在
    public boolean isExist(final String username, final String name) {
        List list 
= (List) getHibernateTemplate().execute(
                
new HibernateCallback() {

                    
public Object doInHibernate(Session session)
                            
throws HibernateException, SQLException {
                        List result 
= session.createCriteria(Address.class)
                                .add(Restrictions.eq(
"username", username))
                                .add(Restrictions.eq(
"name", name)).list();
                        
return result;
                    }

                }
);
        
if (list.size() > 0{
            
return true;
        }

        
return false;
    }


    
// 更新联系人
    public void update(Address address) {
        getHibernateTemplate().update(address);

    }


}

 

//测试类 AddressDAOTest.java

 

package org.kyle.address.dao.impl.test;

import java.util.Iterator;
import java.util.List;

import org.kyle.address.dao.IAddressDAO;
import org.kyle.address.dao.impl.AddressDAO;
import org.kyle.address.hibernate.beans.Address;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import junit.framework.TestCase;

public class AddressDAOTest extends TestCase {
    
public IAddressDAO addressDAO;

    
public void setUp() throws Exception {
        
super.setUp();
        addressDAO 
= new AddressDAO();
        ApplicationContext ctx 
= new FileSystemXmlApplicationContext(
                
"E:/web项目/addressBook/WebRoot/WEB-INF/applicationContext.xml");
        addressDAO 
= (IAddressDAO) ctx.getBean("addressDAO");
    }


    
public void test_Insert() {
        Address address 
= new Address();
        address.setUsername(
"zhaohui");
        address.setName(
"kyle");
        address.setSex(
"2");
        address.setQq(
"123456789");
        address.setMobile(
"13573178648");
        address.setCompany(
"intel");
        address.setPostcode(
"4646454");
        address.setAddress(
"ShangHai");
        addressDAO.insert(address);
        address 
= addressDAO.findById("2");
        assertNotNull(address);

    }


    
public void test_FindAllByUsername() {
        List
<Address> list = addressDAO.findAllByUsername("zhaohui");
        assertTrue(list.size() 
> 0);
        
for (Address address : list) {
            assertNotNull(address);
        }


    }

    
public void test_FindById(){
        Address address
=addressDAO.findById("2");
        assertNotNull(address);
    }

    
public void test_Update(){
        Address address 
= new Address();
        address.setId(
new Integer(2));
        address.setUsername(
"zhaohui");
        address.setName(
"jack");
        address.setSex(
"2");
        address.setQq(
"123456789");
        address.setMobile(
"13573178648");
        address.setCompany(
"Microsoft");
        address.setPostcode(
"4646454");
        address.setAddress(
"ShangHai");
        addressDAO.update(address);
        Address address2
=addressDAO.findById("2");
        assertTrue(address.getCompany().equals(
"Microsoft"));
    }

    
public void test_Delete(){
        addressDAO.delete(
"22");
        Address address
=addressDAO.findById("22");
        assertNull(address);
    }

    
public void test_IsExist(){
        
        
boolean b=addressDAO.isExist("zhaohui""kyle");
        
        assertEquals(b,
true);
    }


}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值