1

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Himanshu Yadav
  • 13,315
  • 46
  • 162
  • 291
  • What exactly is preventing you from running JUnit tests in the same manner as the [hibernate tutorials](http://docs.jboss.org/hibernate/orm/4.1/quickstart/en-US/html/ch04.html) 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 Mar 21 '13 at 16:23
  • 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 Mar 21 '13 at 17:19
  • 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 Mar 21 '13 at 17:40
  • 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 Mar 21 '13 at 17:58
  • @forty-two That link doesn't even mention JUnit! – Chloe May 07 '18 at 19:35
  • @Chloe The point was to configure and run the Entity classes outside of Tomcat. – forty-two May 08 '18 at 09:57

2 Answers2

0

Can you maybe just mock your Datasources?

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

Andres L
  • 683
  • 9
  • 20
0

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();
Holger Thurow
  • 764
  • 4
  • 12