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

类别:Java 点击:0 评论:0 推荐:

20)为SpringappController增加单元测试

l         SpringappController依赖于HttpServletRequest、HttpServletResponse以及应用程序context

l         控制器没有使用request和response,所以将它们简单的设成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目录下,去掉messageSource、urlMapping、viewResolver条目

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是测试getProducts和increasePrice方法,在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());

    }

 

}

本文地址:http://com.8s8s.com/it/it16189.htm