MrMoog
MrMoog

Reputation: 417

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

Upvotes: 1

Views: 789

Answers (2)

Roman C
Roman C

Reputation: 1

You can solve the problem by upgrading to version 2.2.2 generally available on the site full-hibernate-plugin-for-struts2.

With the version are you using the transaction interceptor is unable to put the session on the stack.

Upvotes: 0

MrMoog
MrMoog

Reputation: 417

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

Upvotes: 2

Related Questions