Run JUnit test case in tomcat container

2.1k Views Asked by At

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

There are 2 best solutions below

1
Andres L On

Can you maybe just mock your Datasources?

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

0
Holger Thurow On

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