hibernatestruts2junitstruts2-s2hibernatestruts2-junit-plugin

Struts 2 JUnit plugin + Hibernate


I'm testing my Struts 2 web application using Struts 2 JUnit plugin, following Unit Testing.

In my web application I use fullHibernateCore-plugin-1.4 to integrate Hibernate functionality.

When I test an action that do some Hibernate stuff, it returns NullPointerException.

From what I've understood, since Struts 2 JUnit plugin use a fake container to execute actions, the HibernateSession don't get fired.

How can I solve this problem?

This is a test example:

public class testRegisterAction extends StrutsTestCase {

    public void testGetActionProxy() throws Exception {
        //set parameters before calling getActionProxy
        request.setParameter("user.name", "TestName");
        
        ActionProxy proxy = getActionProxy("/userRegister.action");
        assertNotNull(proxy);

        RegistrationAction action = (RegistrationAction) proxy.getAction();
        assertNotNull(action);
        
}

Solution

  • It wasn't an Hibernate error, but one concerning Struts Tiles plugin. I was using Tiles and I didn't set proxy.setExecuteResult(false), so JUnit was trying to execute all jsp/tiles stuff (after the action return) giving error. In the stacktrace there were also Hibernate errors and so I thought (wrongly) that the fault was of Hibernate stuff.

    I solved setting proxy.setExecuteResult(false).