From: Charles H. <Cha...@Su...> - 2004-02-27 19:34:15
Attachments:
UpdateItemsCommandTest.java
UpdateItemsCommand.java
|
Hey gang, I'm new to MockObjects (and absolutely loving it!!!!). I created a test class which tests a command class which has an execute method that takes an HttpServletRequest object. So I mocked the request and a session (since the command gets a QualityTemplateBuilder object from the session) and I added an attribute called "currentTemplate" to the request. However, when I run my test from ANT, the error message I see is as follows: [junit] Testcase: testExecute(com.sun.es.acac.quality.UpdateItemsCommandTest): FAILED [junit] attributes does not contain currentTemplate [junit] junit.framework.AssertionFailedError: attributes does not contain currentTemplate I'm attaching both the source file to be tested and the test class so that someone can see if they see what I am doing wrong. Thanks for your support! Charles |
From: Jeff M. <je...@mk...> - 2004-03-01 09:49:16
|
You should be using setupGetAttribute not setAttribute. Sorry about this the naming conventions in that area a bit inconsistent. On Fri, 2004-02-27 at 19:23, Charles Hayes wrote: > Hey gang, I'm new to MockObjects (and absolutely loving it!!!!). I > created a test class which tests a command class which has an execute > method that takes an HttpServletRequest object. So I mocked the request > and a session (since the command gets a QualityTemplateBuilder object > from the session) and I added an attribute called "currentTemplate" to > the request. > > However, when I run my test from ANT, the error message I see is as follows: > > [junit] Testcase: > testExecute(com.sun.es.acac.quality.UpdateItemsCommandTest): FAILED > [junit] attributes does not contain currentTemplate > [junit] junit.framework.AssertionFailedError: attributes does not > contain currentTemplate > > I'm attaching both the source file to be tested and the test class so > that someone can see if they see what I am doing wrong. > > Thanks for your support! > > Charles > > ______________________________________________________________________ > package com.sun.es.acac.quality; > > import junit.framework.*; > import com.mockobjects.servlet.MockHttpServletRequest; > import com.mockobjects.servlet.MockHttpSession; > import javax.servlet.http.*; > import java.util.Enumeration; > import java.util.ArrayList; > import java.util.LinkedHashMap; > import java.util.Iterator; > import java.util.Set; > > import com.sun.es.acac.ACACController; > import com.sun.es.common.CommandException; > > public class UpdateItemsCommandTest extends TestCase { > private QualityTemplateBuilder builder = null; > private MockHttpServletRequest request = null; > private MockHttpSession session = null; > > protected void setUp() throws Exception { > super.setUp(); > /**@todo verify the constructors*/ > builder = new QualityTemplateBuilder(); > } > > protected void tearDown() throws Exception { > builder = null; > super.tearDown(); > } > > public void testExecute() { > LinkedHashMap templateItemMap; > request = new MockHttpServletRequest(); > > /////////////////////////////////////////////////////////////// > // Build up a template builder object > builder = new QualityTemplateBuilder(); > builder.setTemplateID("1"); > builder.setCountry("1"); > builder.setGroup("1"); > // Create 2 categories > String [] catParams = {"1", "3"}; > request.setupAddParameter("listB", catParams); > builder.setCategories(request); > // Create 2 items for the first category > String[] items1 = {"1", "2"}; > request.setupAddParameter("listB", items1); > builder.setCategoryItems(request); > // Create 2 items for the second category > String[] items2 = {"10", "13"}; > request.setupAddParameter("listB", items2); > builder.setCategoryItems(request); > > ////////////////////////////////////////////////////////////// > // Add the builder to the HttpServletRequest > request.setAttribute("currentTemplate", builder); > > ////////////////////////////////////////////////////////////// > // Add the request to the session > MockHttpSession session = new MockHttpSession(); > request.setSession(session); > > UpdateItemsCommand command = new UpdateItemsCommand("Summary Screen"); > try { > String next = command.execute(request); > } catch(CommandException ce) { > System.out.println("CommandException thrown from UpdateItemsCommandTest.class"); > } > > } > > } > > ______________________________________________________________________ > /** > * $Log: UpdateItemsCommand.java,v $ > * Revision 1.1 2004/02/27 02:12:44 ch88251 > * Still working on updating quality templates. > * > */ > package com.sun.es.acac.quality; > > import javax.servlet.http.HttpServletRequest; > import javax.servlet.http.HttpSession; > > import com.sun.es.common.Command; > import com.sun.es.common.CommandException; > import com.sun.es.common.DBException; > > import java.util.logging.*; > import java.util.Enumeration; > > /** > * This class loads the existing quality items for a specific category > * into a QualityTemplateBuilder and then redirects the user to a view > * that allows the user to change the items. > */ > public class UpdateItemsCommand implements Command { > > private String next; // The next view to display > private QualityTemplateBuilder builder; > > private Logger log = Logger.getLogger(UpdateItemsCommand.class.getName()); > > /** > * This constructor instantiates a new command and receives information > * about which view to display after the command executes. > * @param next The next view to display > */ > public UpdateItemsCommand(String next) { > this.next = next; > } > > /** > * This method gets the template ID from the request and then > * tells the QualityTemplateBuilder to load all it's values for > * the given ID. > * @param request The request should contain the templateID so that > * we will know which quality template is being edited. > * @return Returns the next view to display > * @throws CommandException If the command is not recognized. > */ > public String execute(HttpServletRequest request) throws CommandException { > HttpSession session = request.getSession(true); > QualityTemplateBuilder builder = > (QualityTemplateBuilder)session.getAttribute("currentTemplate"); > > if (builder != null) { > QualityCategory currentCategory = builder.getCurrentCategory(); > builder.setCategoryItems(currentCategory, request); > } > return next; > } > > } -- jeff martin information technologist mkodo limited mobile: 44 (0) 78 5547 8331 phone: 44 (0) 20 2226 4545 email: je...@mk... www.mkodo.com |
From: Charles H. <Cha...@Su...> - 2004-03-02 16:03:33
|
Thanks a lot Jeff, this took care of the problem. Charles Jeff Martin wrote: >You should be using setupGetAttribute not setAttribute. Sorry about this >the naming conventions in that area a bit inconsistent. > > > >On Fri, 2004-02-27 at 19:23, Charles Hayes wrote: > > >>Hey gang, I'm new to MockObjects (and absolutely loving it!!!!). I >>created a test class which tests a command class which has an execute >>method that takes an HttpServletRequest object. So I mocked the request >>and a session (since the command gets a QualityTemplateBuilder object >>from the session) and I added an attribute called "currentTemplate" to >>the request. >> >>However, when I run my test from ANT, the error message I see is as follows: >> >> [junit] Testcase: >>testExecute(com.sun.es.acac.quality.UpdateItemsCommandTest): FAILED >> [junit] attributes does not contain currentTemplate >> [junit] junit.framework.AssertionFailedError: attributes does not >>contain currentTemplate >> >>I'm attaching both the source file to be tested and the test class so >>that someone can see if they see what I am doing wrong. >> >>Thanks for your support! >> >>Charles >> >>______________________________________________________________________ >>package com.sun.es.acac.quality; >> >>import junit.framework.*; >>import com.mockobjects.servlet.MockHttpServletRequest; >>import com.mockobjects.servlet.MockHttpSession; >>import javax.servlet.http.*; >>import java.util.Enumeration; >>import java.util.ArrayList; >>import java.util.LinkedHashMap; >>import java.util.Iterator; >>import java.util.Set; >> >>import com.sun.es.acac.ACACController; >>import com.sun.es.common.CommandException; >> >>public class UpdateItemsCommandTest extends TestCase { >> private QualityTemplateBuilder builder = null; >> private MockHttpServletRequest request = null; >> private MockHttpSession session = null; >> >> protected void setUp() throws Exception { >> super.setUp(); >> /**@todo verify the constructors*/ >> builder = new QualityTemplateBuilder(); >> } >> >> protected void tearDown() throws Exception { >> builder = null; >> super.tearDown(); >> } >> >> public void testExecute() { >> LinkedHashMap templateItemMap; >> request = new MockHttpServletRequest(); >> >> /////////////////////////////////////////////////////////////// >> // Build up a template builder object >> builder = new QualityTemplateBuilder(); >> builder.setTemplateID("1"); >> builder.setCountry("1"); >> builder.setGroup("1"); >> // Create 2 categories >> String [] catParams = {"1", "3"}; >> request.setupAddParameter("listB", catParams); >> builder.setCategories(request); >> // Create 2 items for the first category >> String[] items1 = {"1", "2"}; >> request.setupAddParameter("listB", items1); >> builder.setCategoryItems(request); >> // Create 2 items for the second category >> String[] items2 = {"10", "13"}; >> request.setupAddParameter("listB", items2); >> builder.setCategoryItems(request); >> >> ////////////////////////////////////////////////////////////// >> // Add the builder to the HttpServletRequest >> request.setAttribute("currentTemplate", builder); >> >> ////////////////////////////////////////////////////////////// >> // Add the request to the session >> MockHttpSession session = new MockHttpSession(); >> request.setSession(session); >> >> UpdateItemsCommand command = new UpdateItemsCommand("Summary Screen"); >> try { >> String next = command.execute(request); >> } catch(CommandException ce) { >> System.out.println("CommandException thrown from UpdateItemsCommandTest.class"); >> } >> >> } >> >>} >> >>______________________________________________________________________ >>/** >> * $Log: UpdateItemsCommand.java,v $ >> * Revision 1.1 2004/02/27 02:12:44 ch88251 >> * Still working on updating quality templates. >> * >> */ >>package com.sun.es.acac.quality; >> >>import javax.servlet.http.HttpServletRequest; >>import javax.servlet.http.HttpSession; >> >>import com.sun.es.common.Command; >>import com.sun.es.common.CommandException; >>import com.sun.es.common.DBException; >> >>import java.util.logging.*; >>import java.util.Enumeration; >> >>/** >> * This class loads the existing quality items for a specific category >> * into a QualityTemplateBuilder and then redirects the user to a view >> * that allows the user to change the items. >> */ >>public class UpdateItemsCommand implements Command { >> >> private String next; // The next view to display >> private QualityTemplateBuilder builder; >> >> private Logger log = Logger.getLogger(UpdateItemsCommand.class.getName()); >> >> /** >> * This constructor instantiates a new command and receives information >> * about which view to display after the command executes. >> * @param next The next view to display >> */ >> public UpdateItemsCommand(String next) { >> this.next = next; >> } >> >> /** >> * This method gets the template ID from the request and then >> * tells the QualityTemplateBuilder to load all it's values for >> * the given ID. >> * @param request The request should contain the templateID so that >> * we will know which quality template is being edited. >> * @return Returns the next view to display >> * @throws CommandException If the command is not recognized. >> */ >> public String execute(HttpServletRequest request) throws CommandException { >> HttpSession session = request.getSession(true); >> QualityTemplateBuilder builder = >> (QualityTemplateBuilder)session.getAttribute("currentTemplate"); >> >> if (builder != null) { >> QualityCategory currentCategory = builder.getCurrentCategory(); >> builder.setCategoryItems(currentCategory, request); >> } >> return next; >> } >> >>} >> >> |