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 Testing an Add() Method With JUnit and EasyMock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Testing an Add() Method With JUnit and EasyMock" Watch "Testing an Add() Method With JUnit and EasyMock" New topic
Author

Testing an Add() Method With JUnit and EasyMock

Jason Ferguson
Ranch Hand

Joined: Sep 16, 2007
Posts: 47
I have figured out quite a bit since my earlier post, but am still stuck on how to test methods that insert, update, or delete a method from the database.
(Query methods I can test pretty well).

I'm working on a CBT application, with a layer system that looks something like this:

- Controller (Struts 1.X Actions)
- Service Layer
- DAO Layer

Just randomly, I decided to start with the Service Layer (actually, not so randomly... my local database on my dev machine isn't configured yet, so I can't test the DAO layer).

Here is what I have so far:

CbtService Interface:



Here's the unit test:



Since I enter reply mode in setUp(), I don't think I can add a new member to cbtList in testAddCbt(). And since I can't add another member there, I can't remove it in testDeleteCbt().

Does anyone have any ideas for testing these methods?
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
First it looks like you're using an ancient version of EasyMock... Is that intentional?

Seems like you're trying to do the wrong things in the setUp method. I'd suggest restructuring it like: (using the a more modern (but still ~2.5 years old EasyMock))




You're only testing are the correct calls proxied through to the DAO (and any other business logic/manipulation you need to do). Since you're mocking the DAO, there's no real need to build up a list of cbts anywhere, I haven't ripped it out yet, but its really not needed... Even for testing exceptions/duplicates, it won't be needed since all you need to do is set the expectation to be expect().andThrow().... to check if you handle those cases correctly.
Jason Ferguson
Ranch Hand

Joined: Sep 16, 2007
Posts: 47
Originally posted by Eric Nielsen:
First it looks like you're using an ancient version of EasyMock... Is that intentional?


Its an unfortunate necessity. EasyMock 2.X requires Java 5.0. It seems that the only way to get my app server upgraded out of the stone age would involve violence, and I've decided that it's not worth ending up in jail for.


Seems like you're trying to do the wrong things in the setUp method. I'd suggest restructuring it like: (using the a more modern (but still ~2.5 years old EasyMock))


// Snipped out code

You're only testing are the correct calls proxied through to the DAO (and any other business logic/manipulation you need to do). Since you're mocking the DAO, there's no real need to build up a list of cbts anywhere, I haven't ripped it out yet, but its really not needed... Even for testing exceptions/duplicates, it won't be needed since all you need to do is set the expectation to be expect().andThrow().... to check if you handle those cases correctly.



Okay, back to the books then. I'm only a couple of days into doing unit testing, and haven't used expect().andThrow() yet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing an Add() Method With JUnit and EasyMock