2

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);
        
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
MrMoog
  • 427
  • 7
  • 18
  • Unit tests shouldn't include the DB layer; are you not mocking your services? – Dave Newton Jan 31 '13 at 14:31
  • Generally I want to test that my actions do what they should do. In this example RegistrationAction write on a DB a new user. – MrMoog Jan 31 '13 at 14:48
  • That's an integration test, because you're testing multiple things. A unit test would check the action against known behavior of your user service, which would be mocked/stubbed to simulate all/most possible conditions the service could exhibit. – Dave Newton Jan 31 '13 at 14:59
  • 1
    There is DBUnit, which is designed for such testing. Use of an in memory database is strongly recommended to keep tests agile. As Dave mentioned DB testing is a form of integration testing and generally shouldn't be done with JUnit and DBUnit despite the name it is an integration test... but the name is cool. – Quaternion Jan 31 '13 at 15:20
  • Instead of editing your question, add the solution as an answer and mark it as accepted. – madth3 Feb 08 '13 at 20:17

2 Answers2

2

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

Roman C
  • 49,761
  • 33
  • 66
  • 176
MrMoog
  • 427
  • 7
  • 18
0

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • No, it works also with 1.4. I thought it was an Hibernate problem, but it wasn't! The problem was caused by Struts Tiles plugin, so now it's ok. – MrMoog Feb 04 '13 at 17:19
  • @MrMoog Ok, describe your problem in the question or in the answer, doesn't matter, I didn't see how the tiles has been concern. – Roman C Feb 04 '13 at 18:21