I had the same problem, here's how I solved it, its a pretty simple base class.
In my case, I use hibernate, so leave the contents of the session / transaction if you are not using it. This will allow you to use all the struts mock objects that I have found convenient. A simple test case follows.
I still need to figure out how to inject resource packages (i18n lines) ... I will add this to the base class.
package com.accelarad.unittest; import org.apache.struts2.StrutsTestCase; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.orm.hibernate3.SessionFactoryUtils; import org.springframework.orm.hibernate3.SessionHolder; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( { "classpath:spring-datasource-unittest.xml", "classpath:spring-test-hibernate.xml", "classpath:spring-context.xml", "classpath:spring-security.xml", "classpath:spring-server.xml" } ) public abstract class AbstractSpringStruts2JUnit4 extends StrutsTestCase implements ApplicationContextAware { protected ApplicationContext applicationContext; @Autowired protected SessionFactory sessionFactory;
Test Case Example:
public class MyActionTest extends AbstractSpringStruts2JUnit4 { @Test public void testOneStrutsAction() throws Exception { ActionProxy proxy = super.getActionProxy("/packageName/actionName"); TestCase.assertEquals("actionName result", "success", proxy.execute()); MyAction myAction = (MyAction ) proxy.getAction(); TestCase.assertNotNull(myAction.getSomething()); } }
You can take it from there, I think.
source share