This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Testing and the fly likes EasyMock'ing JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "EasyMock Watch "EasyMock New topic
Author

EasyMock'ing JPA

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

I've been searching google trying to find examples of using EasyMock to mock up JPA based DAO classes/interfaces. I've found a few things but they all connect to the database. I want to test my DAO's disconnected. I've tried mocking the EntityManagerFactory, JpaTemplate, etc, but there are so many abstractions I'm not able to get them all and I really am not even understanding what I am doing because I just started looking into EasyMock.

Can anyone point me in the right direction?


GenRocket - Experts at Building Test Data
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Gregg,
What are you trying to test? Things like em.find() should be easy to mock because just the entity manager needs mocking. I guess I'm missing the point of the complexity. And a lot of JPA is configuration which doesn't seem to leave much to unit test.


[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
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

Originally posted by Jeanne Boyarsky:
Gregg,
What are you trying to test? Things like em.find() should be easy to mock because just the entity manager needs mocking. I guess I'm missing the point of the complexity. And a lot of JPA is configuration which doesn't seem to leave much to unit test.


Well, that is probably part of my question. I'm not sure how extensive this testing needs to be. I mean, do I need to be testing CRUD methods for every single DAO? And should these tests be disconnected? I was reading Lasse's TDD book and he goes through testing JDBC based DAO's. He mocks the Datasource and the Connection.

In my JPA stuff there is an EntityManagerFactory and EntityManager. Then there is JpaTemplate which all my DAO's extend. JpaTemplate isn't an interface and when I try and mock it EasyMock complains about this.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

So let me post this question another way. I have the following:








What is the best way to test JpaUserDao without connecting to a database?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Gregg,
Thanks. Discussing code is so much easier!

The only method I would bother unit testing in there is the findAll because it has exception handling logic. (Which I hope is oversimplified here for the sake of example.)

The others are one liners where the test mirrors the implementation. I would integration test them all which provides the coverage. That said, they aren't any harder to test than findAll so there is no reason you couldn't.

I don't use Spring, but if I was testing this, I would create a superclass that extends JpaDaoSupport. Let's call it GreggsDaoSupport here. All my DAOs would extend that superclass. Then GreggsDaoSupport could have a method getJpaTemplateWrapper that does have an interface. Alternatively, you could use easyMock's cglib extension to mock out the concrete class. This is mainly a philosophical difference - either approach is find and would work.
 
Consider Paul's rocket mass heater.
 
subject: EasyMock'ing JPA
 
Similar Threads
Unit testing framework for testing servlets
Deprecated method SetReturnValue() in 2.4
Best tools / tutorials to unit test Hibernate 3 / JPA objects?
Creating Unit Tests (with mocks and shunts!)
JPA books