• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Testing an Add() Method With JUnit and EasyMock

 
Jason Ferguson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic