From: <lau...@ma...> - 2002-04-22 17:26:47
|
Hi, I'm having a hard time wrapping my head around this MO concept. I think I need some help. I'm writing a general purpose parameter parser for servlets. Basically, I want the servlet to validate that all required parameters are part of the request. So in my parser tests, I want to use a mock object (a MockHttpServletRequest) to implement the request parameter of my method. Now, my test looks like this: /* * Test for ParameterParser */ package ca.masq.servlet; import junit.framework.TestCase; import junit.framework.Test; import junit.framework.TestSuite; import javax.servlet.http.HttpServletRequest; import com.mockobjects.*; import com.mockobjects.*; import com.mockobjects.util.TestCaseMo; import com.mockobjects.servlet.MockHttpServletRequest; public class ParameterParser_UnitTest extends TestCaseMo { ParameterParser parser; MockHttpServletRequest servletRequest; public ParameterParser_UnitTest(java.lang.String testName) { super(testName); } public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { TestSuite suite = new TestSuite(ParameterParser_UnitTest.class); return suite; } public void setUp() { servletRequest = new MockHttpServletRequest(); } public void testCreateWithNullRequest() { try { parser = new ParameterParser(new String[] {"foo"}, null); fail("Exception expected"); } catch (IllegalArgumentException iae) { assertTrue(true); } } . . . /* * No error occurs here. */ public void testNoMissingParameters() { String[] requiredParams = { "param1", "param2", "param3" }; servletRequest.setupAddParameter("param1", "value1"); servletRequest.setupAddParameter("param2", "value2"); servletRequest.setupAddParameter("param3", "value3"); parser = new ParameterParser(requiredParams, servletRequest); assertTrue("Required parameter missing", parser.getMissingParameters() == null); } } in the testNoMissingParameters() method, I get an error. This kinda tells me that I'm not setting up the servlet properly. Am I correct in my assessment? What am I not understanding at this point? Thanks, L -- Laurent Duperval <mailto:lau...@ma...> You won't strain your eyes if you look at the bright side of things --Winston Churchill |
From: Scott L. <sl...@sl...> - 2002-04-22 18:06:40
|
On Mon, Apr 22, 2002 at 01:25:44PM -0400, lau...@ma... wrote: > in the testNoMissingParameters() method, I get an error. This kinda tells me > that I'm not setting up the servlet properly. Am I correct in my assessment? > What am I not understanding at this point? What sort of error? Your test code looked good to my semi-educated eye. Are you sure it is not working correctly and finding a bug in the code you are testing? If you are just seeing "this test caused an error", you are not seeing all the output junit is producing - it should have more helpful information. How are you running junit? I typically run it as an ant task, configured to say something like that but also produces a report file with more verbose output. -- Scott Lamb |
From: <lau...@ma...> - 2002-04-22 18:52:48
|
On 22 Apr, Scott Lamb wrote: > On Mon, Apr 22, 2002 at 01:25:44PM -0400, lau...@ma... wrote: >> in the testNoMissingParameters() method, I get an error. This kinda tell= s me >> that I'm not setting up the servlet properly. Am I correct in my assessm= ent? >> What am I not understanding at this point? >=20 > What sort of error? Your test code looked good to my semi-educated eye. A= re > you sure it is not working correctly and finding a bug in the code you ar= e > testing? >=20 Sorry, I have to learn to speak properly; I should've said failure: Time: 0.039 There was 1 failure: 1) testNoMissingParameters(ca.masq.servlet.ParameterParser_UnitTest)junit.f= ramework.AssertionFailedError: Required parameter missing=20 =09at ca.masq.servlet.ParameterParser_UnitTest.testNoMissingParameters(Para= meterParser_UnitTest.java:72) This is my code: public ParameterParser(String[] requiredParams, HttpServletRequest aReq= uest) { request =3D aRequest; if (request =3D=3D null) { throw new IllegalArgumentException("Null request received"); } requestParameters =3D request.getParameterMap(); requiredParameters =3D requiredParams; } and the test code (I modified it a bit since my earlier post): public void setUp() { servletRequest =3D new MockHttpServletRequest(); servletRequest.setupAddParameter("param1", "value1"); servletRequest.setupAddParameter("param2", "value2"); servletRequest.setupAddParameter("param3", "value3"); } public void testNoMissingParameters() { String[] requiredParams =3D { "param1", "param2", "param3" }; parser =3D new ParameterParser(requiredParams, servletRequest); assertTrue("Required parameter missing ", parser.getMissingParamete= rs() =3D=3D null); } So I expect request.getParameterMap() to return a Map containing the keys "param1", "param2" and "param3". But when I run in the debugger, it returns null. So I expect there's something I'm misunderstanding. L --=20 Laurent Duperval <mailto:lau...@ma...> La lettre, exsangue et chiffonn=E9e, hors d'haleine mais triomphante, about= it enfin entre les mains de celui qui suscita toute l'extraordinaire odyss=E9e d'=E9motion que je viens de vous narrer: MOI! -Achille Talon |
From: <lau...@ma...> - 2002-04-22 19:33:33
|
On 22 Apr, To: MockObjects Users List wrote: > and the test code (I modified it a bit since my earlier post): > > public void setUp() { > servletRequest = new MockHttpServletRequest(); > servletRequest.setupAddParameter("param1", "value1"); > servletRequest.setupAddParameter("param2", "value2"); > servletRequest.setupAddParameter("param3", "value3"); > } > > public void testNoMissingParameters() { > String[] requiredParams = { "param1", "param2", "param3" }; > parser = new ParameterParser(requiredParams, servletRequest); > assertTrue("Required parameter missing ", parser.getMissingParameters() == null); > } > Ok, so I changed the test code to add this: class TestMockHttpServletRequest extends MockHttpServletRequest { HashMap paramMap = new HashMap(); TestMockHttpServletRequest() { super(); } public void setupAddParameter(String s, String s1) { super.setupAddParameter(s, s1); paramMap.put(s, s1); } public Map getParameterMap() { return paramMap; } } and I use this class as my mock object. I now get the expected behaviour. However, I'm not sure that's the correct way to do things. L -- Laurent Duperval <mailto:lau...@ma...> Murphy's Laws for Frequent Flyers 2. If you are running late for a flight, it will depart from the farthest gate within the terminal. |
From: Vincent T. <vt...@sy...> - 2002-04-23 02:33:13
Attachments:
diff.txt
|
Laurent, Jeff has been busy recently reorganizing the CVS tree. You should get the latest source from CVS. Mocks for servlet 2.3 are located under j2ee/1.3/com/mockobjects/servlet. getParametersMap() is not yet implemented but attached is a patch that does it. Best is to add the expected behavior to the Mock rather than extending it. We implement little by little as our needs grow. Guys, can anyone of you review the patch and apply it if it makes sense (it's pretty late for me now ;-)) -- Vincent On Mon, 2002-04-22 at 19:32, lau...@ma... wrote: > On 22 Apr, To: MockObjects Users List wrote: > > and the test code (I modified it a bit since my earlier post): > > > > public void setUp() { > > servletRequest = new MockHttpServletRequest(); > > servletRequest.setupAddParameter("param1", "value1"); > > servletRequest.setupAddParameter("param2", "value2"); > > servletRequest.setupAddParameter("param3", "value3"); > > } > > > > public void testNoMissingParameters() { > > String[] requiredParams = { "param1", "param2", "param3" }; > > parser = new ParameterParser(requiredParams, servletRequest); > > assertTrue("Required parameter missing ", parser.getMissingParameters() == null); > > } > > > > Ok, so I changed the test code to add this: > > class TestMockHttpServletRequest extends MockHttpServletRequest { > > HashMap paramMap = new HashMap(); > > TestMockHttpServletRequest() { > super(); > } > > public void setupAddParameter(String s, String s1) { > super.setupAddParameter(s, s1); > paramMap.put(s, s1); > } > > public Map getParameterMap() { > return paramMap; > } > } > > and I use this class as my mock object. I now get the expected behaviour. > However, I'm not sure that's the correct way to do things. > > L > > -- > Laurent Duperval <mailto:lau...@ma...> > > Murphy's Laws for Frequent Flyers > 2. If you are running late for a flight, it will depart from the farthest > gate within the terminal. > > > > _______________________________________________ > Mockobjects-java-users mailing list > Moc...@li... > https://lists.sourceforge.net/lists/listinfo/mockobjects-java-users |
From: <lau...@ma...> - 2002-04-23 11:26:08
|
On 22 Apr, Vincent Tence wrote: > getParametersMap() is not yet implemented but attached is a patch that does it. > Best is to add the expected behavior to the Mock rather than extending it. We implement little by > little as our needs grow. > Ok, that's fair. I guess I didn't understand it like that. Thanks, L -- Laurent Duperval <mailto:lau...@ma...> Velilind's Laws of Experimentation: 1. If reproducibility may be a problem, conduct the test only once. 2. If a straight line fit is required, obtain only two data points. |