1
votes

I am writing a JUnit test case which tests a findBy method of a HibernateEnity. Entity gets it's database connection from tomcat managed datasources.
JUnit test case:

MyHibernateEntity entity = new MyHibernateEntity();

expectedResult = entity.findByFK(dummyFK);

How can I run testcase inside tomcat container? We don't have any other Java EE in-container component other then datasources. That's why I need a framework with least footprints. Till now I have tried following:

  1. Arquillian - Too complex to implement. A lot of configuration.
  2. Cactus - It has 3 class to extend. ServletTestCase, JspTestCase and FilterTestCase. Not sure which one is applicable in my case.
  3. DBUnit - Read about it in JUnit in Action. I don't know if it is applicable for in container datasources.

    Note: I understand this test case is not a pure unit testing. I am introducing JUnit in a legacy system and do not have time to resolve integration issues.

2
What exactly is preventing you from running JUnit tests in the same manner as the hibernate tutorials are run? It should be possible to configure data sources outside tomcat. And, surley, the entity classes don't have dependencies to servlets or other J2EE stuff, do they? - forty-two
Entity class gets db connection from a SessionHelper class. Datasources are defined in tomcat container and can not be accessed out side of container. Did you get the problem? - Himanshu Yadav
And SessionHelper is a singleton with a getInstance() method? If that's the case then I understand completely. But wouldn't it be worthwile to make the SessionHelper configurable in that case? - forty-two
You know! that's the reason I do not want to go for mock objects etc. Its a legacy code.Class like SessionHelper is backbone of the system. I am not keen to make any changes to that. - Himanshu Yadav
@forty-two That link doesn't even mention JUnit! - Chloe

2 Answers

0
votes

Can you maybe just mock your Datasources?

https://code.google.com/p/mockito/

0
votes

With TomcatJNDI you can access all JNDI resources from inside your classes as if they would run in Tomcat Server. To make the resources accessible via JNDI lookup you have to write code like

TomcatJNDI tomcatJNDI = new TomcatJNDI();
tomcatJNDI.processContextXml(contextXmlFile);
tomcatJNDI.start();