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.
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.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()); } } }


Reply With Quote
