(20) Increase unit testing for SpringAppController
l SpringAppController depends on HttpServletRequest, HttpservletResponse, and application Context
l The controller does not use Request and Response, so it is simply set to null
l Application Context can be loaded outside of the web container, here 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 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 This is just testing the handleRequest method, check the data returned in Model.
l Load the application context in the setup method; copy the springapp / web-inf / springapp-servlet.xml to the src / web - web-inf directory, remove Messagesource, Urlmapping, ViewResolver entry L (Translator: Eclipse) JUnit, so it is easy to perform unit testing in Eclipse)
(21) Increase unit testing and new features to ProductManager
l Increaseprice in ProducTManager increasing growth price ()
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.ListItIn ();
While (li.hasnext ()) {
Product p = (product) li.next ();
Double newprice = p.getPrice (). doubleValue () * (100 PCT) / 100;
P.SETPRICE (New Double (NewPrice));
}
}
}
l This test case is the test getProducts and Increaseprice methods, and some product information is initialized 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);
}
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 ());
}
}