|
From: <jue...@we...> - 2004-04-27 17:58:44
|
Matt, everybody,=20 Essentially, the Servlet API mocks in Spring's test directory are just = there because the MockObjects (http://www.mockobjects.com) Servlet API = mocks are so inconvenient to use. The current Spring-provided Servlet = API mocks are by no means in a polished state, though: They're just good = enough for being used within the framework test suite. They'd need some = serious refinement for making them public - after all, we have a = reputation to lose :-) So on that occasion, I've reworked those mocks today, making them = significantly more complete than before, and ready for getting included = in the Spring distribution. The question is: Where to include them? I'd = like to include them in the main Spring sources, in a = org.springframework.web.mock package: They would naturally be part of = spring.jar and spring-web.jar then. The test directory (where they = currently reside) is no appropriate place, as it is about testing the = framework rather than providing infrastructure classes.=20 While it isn't in general appropriate to include mocks in the main = Spring codebase, I guess this is a different case: Those mocks are = specifically provided to ease testing of Spring web contexts (like = XmlWebApplicationContext or StaticWebApplicationContext that need a = ServletContext) and controllers (the Controller.handleRequest method = needs an HttpServletRequest and an HttpServletResponse). Including them = in the standard distribution would also indicate our strong focus on = testability. The new MockServletContext implementation uses the = org.springframework.core.io.Resource API for resource loading, to allow = for ServletContext.getResourceAsStream from any resource base. So = partly, the mock implementations depend on low-level Spring API. We = could of course keep the mocks in a separate part of the Spring CVS, but = it's just about a couple of classes, thus hardly worth a separate source = tree. IMO, the main concern regarding additions to the main Spring codebase is = whether those additions can potentially grow significantly: While = Keith's rules API in the sandbox has definite potential there (so had = Ben Alex' security framework that became the Acegi Security System), the = recent Struts support classes and those Servlet API mocks will at best = see slight refinements but certainly not exponential growth. So the = former seems like a candidate for a separate module to me, while the = latter should fit into the main Spring codebase. An important point to consider is that there are simply no mocks = necessary for testing most parts of a typical Spring app, respectively = just interfaces that can conveniently be mocked in a dynamic fashion = with EasyMock. There are just two exceptions, as far I see: Our = SimpleNamingContext stuff eases custom JNDI environments, and the newly = refined set of Servlet API mocks allows for convenient testing of Spring = web MVC (and web contexts). Mocking an EJB container is next to impossible, so that's not worth = further consideration. JDBC can quite nicely be mocked with EasyMock; = it's preferable to use the JdbcOperations interface as far as possible, = though. Same for a Hibernate Session, with HibernateOperations being = preferable. It's more or less just the Servlet API that's really tedious = to mock with EasyMock, due to all those interdependent methods (e.g. URL = paths, parameters, attributes). What does everybody think? Does anyone mind including those polished = Servlet API mocks in the main Spring codebase, already for 1.0.2? Any = suggestions for alternatives? Juergen -----Original Message-----=20 From: spr...@li...=20 [mailto:spr...@li...]On Behalf Of=20 Matt Raible=20 Sent: Saturday, April 24, 2004 2:02 PM=20 To: spr...@li...=20 Subject: Re: [Springframework-user] Testing Controllers and=20 FormControllers=20 I use StrutsTestCase (http://strutstestcase.sf.net), which allows you =20 to write very little code to test an Action. Here is a simple example =20 of testing an "execute" method:=20 package org.appfuse.webapp.action;=20 import servletunit.struts.CactusStrutsTestCase;=20 import org.appfuse.Constants;=20 public class PersonActionTest extends CactusStrutsTestCase {=20 =20 public PersonActionTest(String name) {=20 super(name);=20 }=20 public void testExecute() {=20 // test execute method=20 setRequestPathInfo("/editPerson");=20 addRequestParameter("id", "1");=20 actionPerform();=20 verifyNoActionErrors();=20 assertNotNull(getRequest().getAttribute(Constants.PERSON_KEY)); = }=20 public static void main(String[] args) {=20 junit.textui.TestRunner.run(PersonActionTest.class);=20 }=20 }=20 Since CactusStrutsTestCase extends from Cactus, I can also use Cactus's = FormAuthentication support to mimic container-managed authentication.=20 To test a "Save" is also pretty simple.=20 public void testSave() throws Exception {=20 setRequestPathInfo("/editPerson");=20 addRequestParameter("action", "Edit");=20 addRequestParameter("id", "1");=20 actionPerform();=20 assertTrue(getRequest().getAttribute(Constants.PERSON_KEY) !=3D = =20 null);=20 setRequestPathInfo("/savePerson");=20 addRequestParameter("action", "Save");=20 actionPerform();=20 verifyForward("edit");=20 verifyNoActionErrors();=20 }=20 I think you have something similar to this with the Mock Object you're =20 using in Spring. It just needs to be packaged up as a JAR. I'll be =20 more than happy to write up some documentation on how to use it.=20 Matt=20 On Apr 24, 2004, at 4:27 AM, j=FCrgen h=F6ller [werk3AT] wrote:=20 > Matt,=20 >=20 > I just tried using MockHttpServletRequest and MockHttpServletResponse = > from MockObjects with a Spring FormController. Unfortunately, those =20 > MockObjects mocks are really not well-suited for testing here, as it's = =20 > so much hassle to setup any kind of HttpServletRequest method access. = > What request/response mocks do you use for testing Struts Actions?=20 >=20 > Juergen=20 >=20 >=20 > ________________________________=20 >=20 > Von: spr...@li... im Auftrag von =20 > j=FCrgen h=F6ller [werk3AT]=20 > Gesendet: Sa 24.04.2004 10:57=20 > An: spr...@li...=20 > Betreff: Re: [Springframework-user] Testing Controllers and =20 > FormControllers=20 >=20 >=20 >=20 > I don't see much difference between testing Controllers and =20 > FormControllers: Both share the same "ModelAndView =20 > handleRequest(HttpServletRequest, HttpServletResponse)" method that =20 > you need to invoke for testing. As of 1.0.1, you shouldn't need a =20 > ServletContext mock anymore (except when actually accessing the =20 > ServletContext), so all you need are HttpServletRequest and =20 > HttpServletResponse mocks. The mocks provided by the MockObjects =20 > project should be fine for this.=20 >=20 > Juergen=20 >=20 >=20 > ________________________________=20 >=20 > Von: spr...@li... im Auftrag von =20 > Matt Raible=20 > Gesendet: Fr 23.04.2004 20:48=20 > An: spr...@li...=20 > Betreff: [Springframework-user] Testing Controllers and = FormControllers=20 >=20 >=20 >=20 > I've found it pretty simple to test Controllers, but FormControllers =20 > are=20 > a different story. From looking at the FormControllerTestSuite (URL=20 > below), as well as many other Spring tests, it seems that a lot of=20 > internal mocks are used to test this stuff.=20 >=20 > So my question is - what do you recommend for the average Spring MVC=20 > person? What is the strategy we should be using to test our=20 > controllers? Should we be using the Mocks that Spring provides? This = > would certainly make things easier. If we're supposed to do that - is = > it possible to get these mocks distributed in a JAR?=20 >=20 > Thanks,=20 >=20 > Matt=20 >=20 > http://monkeymachine.co.uk/spring/xref-test/org/springframework/web/=20 > serv=20 > let/mvc/FormControllerTestSuite.html=20 >=20 >=20 >=20 >=20 > -------------------------------------------------------=20 > This SF.net email is sponsored by: The Robotic Monkeys at ThinkGeek=20 > For a limited time only, get FREE Ground shipping on all orders of $35 = > or more. Hurry up and shop folks, this offer expires April 30th!=20 > http://www.thinkgeek.com/freeshipping/?cpg=3D12297=20 > _______________________________________________=20 > Springframework-user mailing list=20 > Spr...@li...=20 > https://lists.sourceforge.net/lists/listinfo/springframework-user=20 >=20 >=20 >=20 >=20 > -------------------------------------------------------=20 > This SF.net email is sponsored by: The Robotic Monkeys at ThinkGeek=20 > For a limited time only, get FREE Ground shipping on all orders of $35 = > or more. Hurry up and shop folks, this offer expires April 30th!=20 > http://www.thinkgeek.com/freeshipping/?cpg=12297=20 > _______________________________________________=20 > Springframework-user mailing list=20 > Spr...@li...=20 > https://lists.sourceforge.net/lists/listinfo/springframework-user=20 >=20 >=20 >=20 >=20 > -------------------------------------------------------=20 > This SF.net email is sponsored by: The Robotic Monkeys at ThinkGeek=20 > For a limited time only, get FREE Ground shipping on all orders of $35 = > or more. Hurry up and shop folks, this offer expires April 30th!=20 > http://www.thinkgeek.com/freeshipping/?cpg=12297=20 > _______________________________________________=20 > Springframework-user mailing list=20 > Spr...@li...=20 > https://lists.sourceforge.net/lists/listinfo/springframework-user=20 -------------------------------------------------------=20 This SF.net email is sponsored by: The Robotic Monkeys at ThinkGeek=20 For a limited time only, get FREE Ground shipping on all orders of $35=20 or more. Hurry up and shop folks, this offer expires April 30th!=20 http://www.thinkgeek.com/freeshipping/?cpg=12297=20 _______________________________________________=20 Springframework-user mailing list=20 Spr...@li...=20 https://lists.sourceforge.net/lists/listinfo/springframework-user=20 |