Develop Spring MVC Applications (4-4) (End)

xiaoxiao2021-03-06  118

(26) Correction of damaged test procedures

l After modifying the ProductManager class, the original TestProductManager needs to make a corresponding modification to test DAO's use.

l First Creating an analog database lasting DAO implementation MockProductManagerDaoImpl class

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 Modify TESTS / Web-InfspringApp-XML, define the reference to the ProductManagerDao interface, and the analog data uses the data defined in the original bean configuration.

- Application Context Definition for "SpringApp" DispatcherServlet.

->

->

Lamp

5.75

Table

75.25

chair

22.79

l Finally modify TestProductManager, use DAO in the setup () method

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

}

}

转载请注明原文地址:https://www.9cbs.com/read-126203.html

New Post(0)