spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
55.28k stars 37.62k forks source link

Create Ant target to package Spring Mocks in a JAR so MVC apps can use to test controllers [SPR-104] #4838

Closed spring-projects-issues closed 20 years ago

spring-projects-issues commented 20 years ago

Matt Raible opened SPR-104 and commented

Since there doesn't seem to be any easy way to test Spring Controller's with plain ol' JUnit or MockObjects, it would be nice if we could use the internal mocks that Spring uses to test its MVC Framework.

This issue is designed to lobby for a "mockjar" target and (possibly) the distribution of spring-mock.jar with the regular releases.


Affects: 1.0.1

spring-projects-issues commented 20 years ago

Matt Raible commented

Hmmm, this JIRA doesn't seem to have the ability to upload patches. Here's a sample target that works to create the needed JAR.

<!--
    Create a JAR of Spring's Mocks for Unit Testing Controllers
-->
<target name="mockjar" depends="initdist,buildtests"
    description="Create a spring-mock.jar file for unit testing controllers">

    <jar jarfile="${dist.dir}/spring-mock.jar">
        <fileset dir="${target.testclasses.dir}">
            <include name="org/springframework/web/mock/**"/>
        </fileset>
        <manifest>
            <attribute name="Spring-Version" value="${spring-version}"/>
        </manifest>
    </jar>
</target>

With this in your classpath, it's much easier to test SimpleFormControllers. For example:

public class UserFormControllerTest extends TestCase { private static Log log = LogFactory.getLog(UserFormControllerTest.class); private ApplicationContext ctx;

public void setUp() throws Exception {
    String[] paths = { "/applicationContext.xml", "/action-servlet.xml" };
    ctx = new ClassPathXmlApplicationContext(paths);
}

public void testDisplayEmptyForm() throws Exception {
    UserFormController c =
        (UserFormController) ctx.getBean("userFormController");
    MockHttpServletRequest request =
        new MockHttpServletRequest(null, "GET", "/editUser.do");
    HttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mv = c.handleRequest(request, response);
    assertTrue("returned correct view name",
               mv.getViewName().equals("userForm"));
}

public void testSaveUser() throws Exception {
    UserFormController c =
        (UserFormController) ctx.getBean("userFormController");
    MockHttpServletRequest request =
        new MockHttpServletRequest(null, "POST", "/editUser.do");
    request.addParameter("firstName", "Matt");
    request.addParameter("lastName", "Raible");

    HttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mv = c.handleRequest(request, response);
    Errors errors =
        (Errors) mv.getModel().get(BindException.ERROR_KEY_PREFIX + "user");
    assertTrue("no errors returned in model", errors == null);
    assertTrue(request.getAttribute("message") != null);
}

public void testSaveUserMissingLastName() throws Exception {
    UserFormController c =
        (UserFormController) ctx.getBean("userFormController");
    MockHttpServletRequest request =
        new MockHttpServletRequest(null, "POST", "/editUser.do");
    request.addParameter("firstName", "Julie");

    HttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mv = c.handleRequest(request, response);
    Errors errors =
        (Errors) mv.getModel().get(BindException.ERROR_KEY_PREFIX + "user");
    assertTrue("errors returned in model", errors != null);
}

}

spring-projects-issues commented 20 years ago

Juergen Hoeller commented

I consider including polished versions of those Servlet API mocks in the 1.0.2 distribution. See mailing list for a discussion.

Juergen

spring-projects-issues commented 20 years ago

Juergen Hoeller commented

There's a new "mock" tree now, containing JNDI mocks (formerly part of the main sources) and reworked web mocks (formerly part of the framework test suite). The build script generates a "spring-mock.jar" into the "dist" directory now.

Juergen