开发Spring MVC应用程序(3-1)

20)为SpringappController增加单元测试

l         SpringappController依赖于HttpServletRequestHttpServletResponse以及应用程序context

l         控制器没有使用requestresponse,所以将它们简单的设成null

l         应用程序context可以在Web容器外加载,这里使用FileSystemXmlApplicationContext

package tests;
    
    
 
    
    
import java.util.Map;
    
    
import java.util.List;
    
    
import java.io.IOException;
    
    
import javax.servlet.http.HttpServletRequest;
    
    
import javax.servlet.http.HttpServletResponse;
    
    
import javax.servlet.ServletException;
    
    
import junit.framework.TestCase;
    
    
import org.springframework.context.ApplicationContext;
    
    
import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    
import org.springframework.web.servlet.ModelAndView;
    
    
import web.SpringappController;
    
    
import bus.Product;
    
    
 
    
    
public class TestSpringappController extends TestCase {
    
    
 
    
    
    private ApplicationContext ac;
    
    
 
    
    
    public void setUp() throws IOException {
    
    
        ac = new FileSystemXmlApplicationContext("src/tests/WEB-INF/springapp-servlet.xml");
    
    
    }
    
    
 
    
    
    public void testHandleRequest() throws ServletException, IOException {
    
    
        SpringappController sc = (SpringappController) ac.getBean("springappController");
    
    
        ModelAndView mav = sc.handleRequest((HttpServletRequest) null, (HttpServletResponse) null);
    
    
        Map m = mav.getModel();
    
    
        List pl = (List) ((Map) m.get("model")).get("products");
    
    
        Product p1 = (Product) pl.get(0);
    
    
        assertEquals("Lamp", p1.getDescription());
    
    
        Product p2 = (Product) pl.get(1);
    
    
        assertEquals("Table", p2.getDescription());
    
    
        Product p3 = (Product) pl.get(2);
    
    
        assertEquals("Chair", p3.getDescription());
    
    
    }
    
    
 
    
    
}
    
    

l         这里只是测试handleRequest方法,检查Model中返回的数据

l         setUp方法中加载应用程序context;将springapp/WEB-INF/springapp-servlet.xml拷贝到src/tests//WEB-INF目录下,去掉messageSourceurlMappingviewResolver条目

l         (译者:Eclipse包含了JUnit,所以很容易在Eclipse进行单元测试)

21)增加单元测试和新功能到ProductManager

l         ProductManager中增加了增长价格的方法increasePrice()

package bus;
    
    
 
    
    
import java.io.Serializable;
    
    
import java.util.ListIterator;
    
    
import java.util.List;
    
    
 
    
    
public class ProductManager implements Serializable {
    
    
 
    
    
    private List products;
    
    
 
    
    
    public void setProducts(List p) {
    
    
        products = p;
    
    
    }
    
    
 
    
    
    public List getProducts() {
    
    
        return products;
    
    
    }
    
    
 
    
    
    public void increasePrice(int pct) {
    
    
        ListIterator li = products.listIterator();
    
    
        while (li.hasNext()) {
    
    
            Product p = (Product) li.next();
    
    
            double newPrice = p.getPrice().doubleValue() * (100 + pct)/100;
    
    
            p.setPrice(new Double(newPrice));
    
    
        }
    
    
    }
    
    
 
    
    
}
    
    

l         这个Test Case是测试getProductsincreasePrice方法,在setUp()方法中初始化一些产品信息

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值