Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Test Controllers

  1. #1
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default Test Controllers

    Maybe this is a stupid question, but somebody have to pose stupid questions also...

    Is it at all possible to JUnit-test Spring controllers with JUnit but without HttpUnit and/or Cactus? And if it is, are there any "best practice"?
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Yes, it's quite easy:

    1. create controller and populate its properties or constructor args in your JUnit test
    2. drive its public entry method using a mock request and response
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default Simple example

    Here's a simple example. You'll need spring-mock.jar in your classpath.

    Controller Test:

    Code:
    package org.appfuse.web;
    
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import junit.framework.TestCase;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.mock.web.MockServletContext;
    import org.springframework.web.context.support.XmlWebApplicationContext;
    import org.springframework.web.servlet.ModelAndView;
    
    public class UserControllerTest extends TestCase {
        private static Log log = LogFactory.getLog(UserControllerTest.class);
        
        private XmlWebApplicationContext ctx;
    
        public void setUp() {
            String[] paths = {"/WEB-INF/applicationContext*.xml",
                              "/WEB-INF/action-servlet.xml"};
            ctx = new XmlWebApplicationContext();
            ctx.setConfigLocations(paths);
            ctx.setServletContext(new MockServletContext(""));
            ctx.refresh();
        }
    
    	public void testGetUsers() throws Exception {
    		UserController c = (UserController) ctx.getBean("userController");
    		ModelAndView mav = c.handleRequest((HttpServletRequest) null,
    				(HttpServletResponse) null);
    		Map m = mav.getModel();
    		assertNotNull(m.get("users"));
    		assertEquals(mav.getViewName(), "userList");
    	}
    }
    More examples can be found in AppFuse: http://tinyurl.com/6ansy

    HTH,

    Matt

  4. #4
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default

    Wow!
    That's amazingly easy, can't wait to get back to work to try it.

    Thanks!
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  5. #5
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default Testing with EasyMock

    As Rod suggests, you can also test your controllers w/o talking to Spring's ApplicationContext at all. For instance, here's the same test using Easy Mock.

    Code:
    public class UserControllerEMTest extends TestCase {
        private static Log log = LogFactory.getLog(UserControllerEMTest.class);
        private MockControl control = null;
        private UserManager mockManager = null;
        private UserController c = null;
    
        protected void setUp() throws Exception {
            control = MockControl.createControl(UserManager.class);
            mockManager = (UserManager) control.getMock();
            c = new UserController();
            c.setUserManager(mockManager);
        }
    
        public void testGetUsers() throws Exception {
            mockManager.getUsers();
            control.setReturnValue(new ArrayList());
            control.replay();
    
            ModelAndView mav =
                c.handleRequest((HttpServletRequest) null,
                                (HttpServletResponse) null);
            Map m = mav.getModel();
            assertNotNull(m.get("users"));
            assertEquals(mav.getViewName(), "userList");
            control.verify();
        }
    }

  6. #6
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default

    OK. Is this the easymock-package that you are using here?
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  7. #7
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default EasyMock

    Quote Originally Posted by kantorn
    OK. Is this the easymock-package that you are using here?
    Yes, from http://easymock.org. The code above uses EasyMock 1.1 (download).

  8. #8
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default

    OK.

    Many thanks!
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  9. #9
    Join Date
    Nov 2004
    Posts
    6

    Default Running Cactus:jdbc.properties values are not visible...

    I've got my web app built with spring 1.1.1 where Dao implemented with iBatis 2.x. DataSource is jdbc based, where all credentials placed into jdbc.properties file. Below is the fragment from the dataAccessContext-local.xml file. It works fine. All xml files are in /WEB-INF/classes folder. So by default, they are visible to the Tomcat classloader. All spring context files, including the mock context are listed in web.xml file. All dependent libraries in /WEB-INF/lib folder.

    The problem appears if I use Cactus bundled with that web app. When I'm trying to deploy this test web app, I've got the error message, where all jdbc credentials are not substituted by the jdbc.properties values and treated by dbcp as is, so they look similar to the below:
    ${jdbc.driverClassName}
    ....
    ${jdbc.password}

    Question: Why jdbc.properties values are not visible when running with Cactus???

    Thanks in advance!!!
    ------------------------------------------------------------------------------


    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="location"><value>classpath:jdbc.properties</value></property>
    </bean>


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>${jdbc.driverClassNa me}</value></property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
    <property name="validationQuery"><value>select 1 from dual</value></property>
    </bean>

  10. #10
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    Also, in terms of configuring mock HttpServletRequest objects, you can also subclass MockHttpServletRequest and provide your own setParameters(Map) method that simply calls addParameters -- since addParameters doesn't follow bean name conventions.

    You can then configure mock http request objects in Spring instead of having request parameters set using Java code in a JUnit.

    e.g.

    Code:
    <bean id="base-post" class="com.myco.test.MockRequest">
      <property name="method"><value>POST</value></property>
      <property name="session"><ref local="test-session"/></property>
      <property name="servletPath"><value>/myapp</value></property>
    </bean>
    
    <bean id="myPost" parent="base-post" singleton="false">
      <property name="requestURI"><value>do-something.htm</value></property>
      <property name="parameters">
        <props>
          <prop key="_target3">Find</prop>
        </props>
      </property>
    </bean>

Similar Threads

  1. help with testing controllers
    By pir8ped in forum Web
    Replies: 9
    Last Post: Apr 28th, 2006, 06:13 PM
  2. Replies: 5
    Last Post: Aug 1st, 2005, 06:30 PM
  3. Base Unit Test for Controllers?
    By ryan.tyer in forum Web
    Replies: 1
    Last Post: Apr 28th, 2005, 06:34 PM
  4. Portlet Form Controllers
    By johnalewis in forum Web
    Replies: 4
    Last Post: Mar 11th, 2005, 10:26 AM
  5. Test controllers
    By brianstclair in forum Web
    Replies: 2
    Last Post: Sep 26th, 2004, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •