Steve,
Thanks so much for your quick reply, I really appreciate you taking the
time to respond.
As it turned out, the expectAndReturn call ended up being slightly
different to what you suggested because the return value of
getParameterNames is an Enumeration, not an Object array....
Here is the snippet of code that worked for me....
Mock mockRequest = new
Mock(javax.servlet.http.HttpServletRequest.class);
Hashtable hash = new Hashtable();
hash.put("PARAM1","Value1");
mockRequest.expectAndReturn("getParameter","Param1",null);
mockRequest.expectAndReturn("getParameter","PARAM1","value1");
mockRequest.expectAndReturn("getParameterNames", hash.keys());
It seems to me that with the Mock HttpServletRequest, I have to predict all
the calls that will be made to the mock object. In the case of the code
above, the calls were made by a utility method that parses
httpRequestParameters and returns the value regardless of case, that is why
the getParameter snippet above had to be called twice.
Is there any way to avoid having to predict all the calls that will be made?
Additionally, I'd like to contribute my Case Study of using the older
MockHttpServletRequest object along with using the new dynamic Mock in the
same context, what is the protocol within this group for making such a
contribution. Do you simply want me to create my own topic within the wiki
or do you want the entire code from my Case Study possibly put into an
examples package? I have a unit test class that has 9 distinct tests for
the utility parseParameter method I wrote in order to study implementation
of mock objects.
cheers
Tony
>>I've used the j2ee v1.2 MockHttpServletRequest and added code to the [..]
>>that had methods, if the method you wanted wasn't implemented, you added
>>an implementation.
>
>exactly!
>
>>Now I'm trying to use the latest j2ee mock objects
>>(mockobjects-jdk1.4-j2ee1.3-0.09.jar) which require the use of Dynamic
>>Mock objects. I've followed the documentation to be able to test the
>>getParameter method but have no idea of how to set up the
>>getParameterNames method.
>
>I woudl guess that you use expectAndReturn("getParameterNames",
> new Object[] {"one", "two"});
>
>>I cannot find any documentation or examples of dynamic mock objects that
>>goes beyond the simple example above and am subsequently stuck. Any help
>>is appreciated, otherwise I'll go back to the j2ee 1.2 mock library, I
>>was really hoping to move forward with the new dynamic approach but the
>>lack of examples and my inexperience with dynamic mocks is holding me up.
>
>it's still a work in progress, which is why we haven't packaged it up yet.
>The basics are quite simple, it tries to match a method call and return a
>value if appropriate. If an expectation is set it will check that,
>otherwise it'll just match. The rest is a rather messy implementation that
>still needs cleanup.
|