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 How to test persistence layer of a web based application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How to test persistence layer of a web based application?" Watch "How to test persistence layer of a web based application?" New topic
Author

How to test persistence layer of a web based application?

Yan Lee
Ranch Hand

Joined: Sep 15, 2003
Posts: 94
Hi,
We have a web based application(packaged as ear) running in Weblogic 7.0.

I want to write a suite of tests to test our persistence operations.
The persistence layer is implemented as DAO's.
There are no EJB's in the application.

How can I invoke these DAO classes packaged in the EAR to test them?
What are the options(frameworks)?

Thanks in advance.
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
As long as your DAO's are well-defined and all your persistence code is in DAO's, you should be able to use JUnit to test your persistence layer
Yan Lee
Ranch Hand

Joined: Sep 15, 2003
Posts: 94
Hi Jayesh,
Thanks for the respose. I do have the following additional question,
How can I invoke the DAO (a plain java class) that is in the .ear from outside of weblogic? The DAO is not in a session bean.

Any links to examples or resources to how this can he done will be highly helpful.

Thanks
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by p intelli:
How can I invoke the DAO (a plain java class) that is in the .ear from outside of weblogic? The DAO is not in a session bean.
You can't, unless you expose the DAO to the outside world using some kind of an adapter like RMI, EJB, web services, servlet, etc.


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

Joined: Nov 05, 2004
Posts: 502
Personally, I would Unit test the DAO outside your web server. You are trying to make the DAO reuseable, right? This means that they should run in standalone apps too, right? So, If I were you I would test my persisitence layer in a standalone JUnit app
B Rook
Greenhorn

Joined: Dec 27, 2003
Posts: 13
I have a related question. I have a DAO with several methods. I want to write a suite of tests to test the success and failure of each of these methods. My DAO is written so that it is passed a DataSource object in its constuctor and it can create a Connection object from that DataSource.

i.e.


How do I create a DataSource in JUnit so that I can test the DAO? Would I have to use Cactus? How would I create the DS in cactus?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30532
    
150

Since DataSource is an interface, you could pass in a mock datasource. This could use easyMock, jMock or even be a class you write yourself.

The mock datasource would return a connection using whatever mechanism you have for getting connections.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
hi,
looks like my question is also alike
i ahve an application with mvc desing pattern
Controller - > helper class - > Dao Implementaion
i was writing test cases for helper classes. but the helper class calls the dao implementaion and in the dao implementaion i am using the data source to get connection. when i run the test on my machine it fails since the daoimpl looks for the data source. Wondering how to write test cases for helper classes any help to any article will be appreciated.
Thanks
Rashid
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30532
    
150

Rashid,
When unit testing the helper class, you want to test just that class. So you would create a mock DAO. What does the helper class do? Think about what it is responsible for to figure out what you are trying to test.

If you were writing a test for the DAO, you would create a mock datasource.
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
Thanks Jeanne
my source code is organised as follows
src->com->eds->met->Helper->all Helper Classes example ValueHelper.java
src->com->eds->met->persistence->all Dao classes.
Now the thing is if i have to write a mock dao where should i write it
i have the following test folder structure in exact manner as the source from the root directory
test->com->eds->met->Helper->ValueHelperTest.java
In the ValueHelperTest i have all the test cases defined for ValueHelper class. In the setUp and teardown method i instantiate the valueHelper object and call the method. if i write my own mock Dao where should i write it.
Please let me know
Thanks...
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30532
    
150

Rashid,
I wouldn't actually advise writing a mock DAO. Just have your real DAO implement an interface. This interface goes in the same package as the original DAO.

Then you can use a framework like easyMock or jMock to mock it out. The framework does all the work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to test persistence layer of a web based application?