开发Spring MVC应用程序(4-4) (完)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

(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());

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航