Announcement Announcement Module
Collapse
No announcement yet.
Cactus + Spring Integration Tests HOWTO Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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

        Comment


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

          Comment


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

            Comment


            • #7
              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

              Comment


              • #8
                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.

                Comment

                Working...
                X