File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Testing and the fly likes Conceptual questions concerning Unit tests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Conceptual questions concerning Unit tests" Watch "Conceptual questions concerning Unit tests" New topic

Conceptual questions concerning Unit tests

chuan ito

Joined: Apr 22, 2008
Posts: 6

Im just trying to setup an unit testing environment in the project im working atm.

I just evaluated the different approaches of mock objects and the idea behind JUnit and the in-container approach of Cactus and i still have some questions about it...

If i want to test my DAO classes which retrieve information from a persistent environment (in this case DB) i use a datasource that i lookup through JNDI. JNDI however is a naming service provided by the container. One thing i want to avoid is to create mock objects for all the services, as this would result in lots of mock objects to maintain.

The in-container approach seems to be more logic to me. What do you guys think?

And how can i just test my DAO classes with the JNDI lookup with Cactus?
Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2851

Hi Chuan, welcome to Javaranch

im working atm.

these are not real words UseRealWords
Also check the ask good questions link in my signature to see if you have posted the topic in the correct forum, and other beginner queries .

Hope this helps
[ April 22, 2008: Message edited by: Amit Ghorpade ]

|Asking Good Questions|
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Welcome to JavaRanch!

First of all, note that you are talking about integration tests rather than unit tests. A unit test does not access the database.

Before you complicate things with Cactus, see if pure JUnit will work for you. Can you pass the connection object directly to the DAO? If so, you don't need JNDI for the test which means you can write a regular JUnit test without any other tools.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
There are unit tests and then there are integration tests. Unit tests are isolated test of just the specific class (or limited set of classes) that you are working on, just a DAO class or just a service class, etc. Integration testing is when you put the classes in a more real-world test harness like cactus and other in-container testing.

For mocking in unit tests I use JMock ( which makes the creation of mock objects (with assertions and constraints ) trivial and you dont have to create a lot of extra mock object classes.

DAOs can often be an edge case that are difficult to unit test in a really meaningful manner, but you can still make sure that all the external pieces used are routing things properly.

Good luck and I hope this helps at least a little.

- Chris Stehno, SCPJ
I agree. Here's the link:
subject: Conceptual questions concerning Unit tests
It's not a secret anymore!