Results 1 to 8 of 8

Thread: Cactus + Spring Integration Tests HOWTO

  1. #1
    Join Date
    Jul 2005
    Posts
    28

    Default Cactus + Spring Integration Tests HOWTO

    I have gotten Spring and Cactus to happily work together and I thought I would post a little how-to for those wanting to use it for integration tests.

    Note that I have only used the ServletTestCase class from Cactus.

    The first thing you will want to do is to go get Cactus. The instructions on the site are well written and you should be able to follow them. The biggest problem I had was putting the cactus.properties file into an appropriate place in the classpath.

    Once you have cactus installed, you will need to start your container. This can be done either manually, thru ANT, or with an IDE. I'm using WSAD so I start my container with my IDE.

    The hardest part of getting Cactus to work with Spring was to figure out how to get my beans to be visible in my Cactus test. The below code snippet was the key for me. See specifically the setup() method. Note that the test itself is pretty bad, but it is for example purposes only.

    Code:
    public class TestMyService extends ServletTestCase
    {
        WebApplicationContext ctx;
        /**
         * Constructor for TestMyService.
         * @param arg0
         */
        public TestMyService(String arg0)
        {
            super(arg0);
        }
        /*
         * @see TestCase#setUp()
         */
        protected void setUp() throws Exception
        {
            //config is an instance of 
            //org.apache.cactus.server.ServletConfigWrapper
            //which inherits from javax.servlet.ServletConfig
        ctx = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
        }
        /*
         * @see TestCase#tearDown()
         */
        protected void tearDown() throws Exception
        {
            super.tearDown();
        }
        
        public void testXXX()
        {
            Bean1 bean1 = (Bean1)ctx.getBean("bean1Service");
            Bean2 bean2 = (Bean2)ctx.getBean("bean2Service");
            Map types = bean1.getTypes();
            User uesr = bean2.getUser("user1", "pass1"));
            for(Iterator iterator = types.entrySet().iterator(); iterator.hasNext();)
            {
                Map.Entry entry = (Map.Entry)iterator.next();
                System.out.println(entry.getKey() + " = " + entry.getValue());
            }
        }
    }
    The best thing about this is that I don't have to worry about where my Spring config files are, or how they are configured. Getting the already configured WebApplicationContext solves that biggest pain.

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

    Default

    Yes, this is clearly the correct approach to getting Cactus to know about Spring. Thanks for posting it here and sharing your experience.

    Have you also looked at the Spring integration test package, org.springframework.test, shipped in spring-mock.jar? This is designed to run integration tests outside the application server, which is many times faster and doesn't involve deploying any test infrastructure to the server. As a bonus, the test superclasses automatically create and rollback transactions, so you don't need to worry about side effects and data modification.

    There's content on this in the testing chapter of the Reference Manual.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Jul 2005
    Posts
    28

    Default

    Hi Rod,
    Yes, we have looked at the integration tests provided in the org.springframework.test package. In our situation, they won't work because we are not using a database, instead, our DAOs map to a CCI resource. So in this case we had to find an alternative. Cactus seemed to be an appropriate option for the integration tests with CCI. If we had been using a database instead, we definitely would have setup a local DB and used the test parent classes that Spring provides for integration tests.

    Thank you for your feedback.

  4. #4
    Join Date
    Jan 2006
    Posts
    11

    Default I get a null application context

    I am a fairly newbie to Tomcat/Spring/Cactus and I am trying to get it all to work. Until the introdution of Spring everything was fine (it still is...). I found your nice advice and I am trying to follow it. But the following statement:

    ctx = WebApplicationContextUtils.getWebApplicationContex t(config.getServletContext());

    returns null because the following statement returns null:
    Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APP LICATION_CONTEXT_ATTRIBUTE);

    in org.springframework.web.context.support.WebApplica tionContextUtils.

    It looks like there is some configuration matter I didn't quite get right. Some attribute stuff...?
    Could you please tell me where I haven't supplied Cactus/Spring/Tomcat with enough information.
    (I am trying to follow Thomas Risberg's step-by-step approach which so far works well)

    best regards
    Øyvind

  5. #5

    Default

    I am also getting this problem what is to be done here.

  6. #6

    Default

    What i have found out is that there should be an attribute called "Webapplicationcontext.ROOT" in serveletcontextwrapper which is not there.

  7. #7
    Join Date
    Aug 2007
    Posts
    1

    Default

    Quote Originally Posted by aamol View Post
    What i have found out is that there should be an attribute called "Webapplicationcontext.ROOT" in serveletcontextwrapper which is not there.
    I am having the same problem (WebApplicationContextUtils.getWebApplicationConte xt(config.getServletContext()); returns null).

    How do I set the Webapplicationcontext.ROOT?

    Thanks

  8. #8
    Join Date
    Jul 2008
    Posts
    6

    Default

    Hi,

    Has anyone got this integration to work ?

    I am attempting to get access to my beans (ejb3) which are defined in my applicationContext.xml

    The following returns null.

    //config is an instance of
    //org.apache.cactus.server.ServletConfigWrapper
    //which inherits from javax.servlet.ServletConfig
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContex t(config.getServletContext());

    .
    .
    .
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/context/applicationContext.xml</param-value>
    </context-param>
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListe ner
    </listener-class>
    </listener>
    .
    .
    .

    Thanks.

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Replies: 2
    Last Post: Sep 20th, 2005, 01:39 AM
  3. Replies: 6
    Last Post: Jun 1st, 2005, 05:40 PM
  4. Replies: 2
    Last Post: May 26th, 2005, 02:30 AM
  5. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 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
  •