Struts2; keeping a session open for StrutsSpringTestCase JUnit tests

后端 未结 1 379
星月不相逢
星月不相逢 2021-01-21 05:14

My project architecture is Struts2 with Spring integration and JPA/Hibernate. StrutsSpringTestCase base class is utilized for JUnit integration tests.

Under normal circu

相关标签:
1条回答
  • 2021-01-21 05:52

    You can put @Transactional annotation over test method and you need to run your tests with spring in order it could find @Transactional annotation. To use JUnit4 in Struts2 tests you need to extend StrutsSpringJUnit4TestCase. So your test class should look something like that:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:applicationContext.xml"})
    public class ActionTest extends StrutsSpringJUnit4TestCase {
      @Transactional
      @Test
      public void testHelperActionLoggedIn() throws Exception {
        // ...
      }
    }
    

    Note: If you need to obtain ActionProxy you can get it by calling getActionProxy method. You probably need to create new session map for it and then you can call execute.

    ActionProxy actionProxy = getActionProxy("/action");
    Map<String, Object> sessionMap = new HashMap<String, Object>();
    actionProxy.getInvocation().getInvocationContext().setSession(sessionMap);
    actionProxy.execute();
    

    BUT if you don't need reference to ActionProxy then you can use executeAction method to execute action in this way you don't need to create new session map.

    executeAction("/action");
    
    0 讨论(0)
提交回复
热议问题