Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
开发Spring MVC应用程序(4-4) (完)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

26)修正破坏的测试程序

l         在修改ProductManager类后,原来的TestProductManager需要作相应的修改来测试DAO的使用

l         首先创建模拟数据库持久的DAO实现MockProductManagerDaoImpl

package tests;
 
import bus.Product;
import java.util.List;
import db.ProductManagerDao;
 
public class MockProductManagerDaoImpl implements ProductManagerDao {
 
    private List products;
 
    public void setProducts(List p) {
        products = p;
    }
 
    public List getProductList() {
        return products;
    }
 
    public void increasePrice(Product prod, int pct) {
        double newPrice = prod.getPrice().doubleValue() * (100 + pct)/100;
        prod.setPrice(new Double(newPrice));
    }
 
}

l         修改tests/WEB-INFspringapp-xml,定义ProductManagerDao接口的引用,模拟数据采用原来Bean配置中定义的数据

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 
<!--
  - Application context definition for "springapp" DispatcherServlet.
  -->
 
 
<beans>
    <bean id="springappController" class="web.SpringappController">
        <property name="productManager">
            <ref bean="prodMan"/>
        </property>
    </bean>
 
    <bean id="prodManDao" class="tests.MockProductManagerDaoImpl">
        <property name="products">
            <list>
                <ref bean="product1"/>
                <ref bean="product2"/>
                <ref bean="product3"/>
            </list>
        </property>
    </bean>
 
    <bean id="prodMan" class="bus.ProductManager">
        <property name="productManagerDao">
            <ref bean="prodManDao"/>
        </property>
<!--
        <property name="products">
            <list>
                <ref bean="product1"/>
                <ref bean="product2"/>
                <ref bean="product3"/>
            </list>
        </property>
-->
    </bean>
 
    <bean id="product1" class="bus.Product">
        <property name="description"><value>Lamp</value></property>
        <property name="price"><value>5.75</value></property>
    </bean>
        
    <bean id="product2" class="bus.Product">
        <property name="description"><value>Table</value></property>
        <property name="price"><value>75.25</value></property>
    </bean>
 
    <bean id="product3" class="bus.Product">
        <property name="description"><value>Chair</value></property>
        <property name="price"><value>22.79</value></property>
    </bean>
 
</beans>

l         最后修改TestProductManager,在setUp()方法中使用DAO

package tests;
 
import java.util.List;
import java.util.ArrayList;
import junit.framework.TestCase;
import bus.ProductManager;
import bus.Product;

 

public class TestProductManager extends TestCase {
 
    private ProductManager pm;
 
    public void setUp() {
        pm = new ProductManager();
        Product p = new Product();
        p.setDescription("Chair");
        p.setPrice(new Double("20.50"));
        ArrayList al = new ArrayList();
        al.add(p);
        p = new Product();
        p.setDescription("Table");
        p.setPrice(new Double("150.10"));
        al.add(p);
        //pm.setProducts(al);
        MockProductManagerDaoImpl pmdao = new MockProductManagerDaoImpl();
        pmdao.setProducts(al);
        pm.setProductManagerDao(pmdao);
        pm.getProducts();
    }
 
    public void testGetProducs() {
        List l = pm.getProducts();
        Product p1 = (Product) l.get(0);
        assertEquals("Chair", p1.getDescription());
        Product p2 = (Product) l.get(1);
        assertEquals("Table", p2.getDescription());
    }
 
    public void testIncreasePrice() {
        pm.increasePrice(10);
        List l = pm.getProducts();
        Product p = (Product) l.get(0);
        assertEquals(new Double("22.55"), p.getPrice());
        p = (Product) l.get(1);
        assertEquals(new Double("165.11"), p.getPrice());
    }
 
}



相关文章

相关软件