File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes Unit testing database connections in container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Unit testing database connections in container" Watch "Unit testing database connections in container" New topic
Author

Unit testing database connections in container

Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
I'm trying to create unit tests that are able to work with a DAO that relies on a datasource managed by my web server. I have looked at in container options like Cactus, but I don't see that this handles my need. Has anyone dealt with a similar problem in the past?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
So which unit you want to test -- the DAO or the class that uses a DAO?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
I'm trying to unit test the DAO itself. I am able to unit test the other classes with StrutsTestCase.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Ok. So how does this problem of yours exhibit itself? I suppose you get some kind of an exception? Or is it something about transactional boundaries?
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
I get a null exception error since my unit test is not occuring "in container". When the actual application runs, everything is fine. This is not acceptable though, since a unit test is required to show the DAO is working correctly.

So I'm wondering if there is a way to do an in container test on my DAO, or possibly a mock object approach that would replace my container specific data connection with an independant data connection...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
It's probably possible to use a mock.

Can you show us the code where the NPE occurs?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
It fails on the call to getConnection. The code looks like this.



This is the proper way to create the connection when running in JRun. DEVSERVER is a managed datasource that is actually configured in JRun. Then JRun automatically handles pooling and other nifty features...

Since it is handled by JRun it does not exist outside the container and so regular unit tests fail...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Instantiate the InitialContext outside of the DAO and pass it in where you initialize the application (for example via the constructor). The test then can pass in a mock Context instead.

Would that work for you?
Mike Wislocki
Greenhorn

Joined: Aug 22, 2005
Posts: 1
I also had a similar problem, but got a little further. I can get my initial context, however I fail on the lookup:



In my test case, I must set the context factory in order to get the initial context


The problem I run into now is I get the exception:

[junit] javax.naming.NameNotFoundException: Name java:comp is not bound in this Context

Anyone have any thoughts on this?
-M
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Instead of org.apache.naming.java.javaURLContextFactory use a Mock implementation that is fully under your control and can be made to behave exactly the way you need for the tests.

Google for "mock objects" for more info.
Michael Bronshteyn
Ranch Hand

Joined: Mar 26, 2002
Posts: 85
Mock objects are great thing, but management people don't always understand what they do. Thus, if they don't see records in the database they may get concerned.

You can try to use local database and use your DAO objects to store and read data there from JUnit test classes. You would need to override the class which gets the connection to the database so you get your local connection. I used this approach for one of my projects and worked fine.


Michael
SCJP2
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Michael Bronshteyn:
Mock objects are great thing, but management people don't always understand what they do. Thus, if they don't see records in the database they may get concerned.


I don't think that JUnit tests are a very good medium to communicate to business people, anyway. I'd prefer FitNesse in such a case...

You can try to use local database and use your DAO objects to store and read data there from JUnit test classes. You would need to override the class which gets the connection to the database so you get your local connection. I used this approach for one of my projects and worked fine.


The biggest drawback I can see is that such kinds of tests are typically dead slow...
 
wood burning stoves
 
subject: Unit testing database connections in container