views:

281

answers:

2

Hi all, I wrote a method that retrieves certain list of strings, given a correct string key. Now when I create a list(the one to be retrieved by method descibed in previous sentence) and create test I can easily get results and test passes successfully.

Now on the other hand if I save the content of this list to database in 2 columns, key and value I wrote a class which retrieves this items with method inside it. And when I print it out to console the expected results are correct, now I initialize my DAO from application context where inside its bean it gets session and because of DAO works.

Now I'm trying to write a test which will mock the DAO, because I'm running test localy not on the server .. so I told jmock to mock it :

private MyDAO myDAO;

in the setup()

myDAO = context.mock(MyDAO.class);

I think I'm mocking it correctly or not, how can I mock this data from database? what is the best way?

Is there somewhere good Jmock documentation? on their official site its not very good and clear, you have to know what you seek in order to find it, can't discover something cool in the mean time.

OR can someone help me with this approach :

How can I create application context which I will use just for tests, to instansiate DAO and few beans there like on server. So I can use it in the tests? Also suggestions, explanation all is welcome . thank you

A: 

Two questions? Is MyDAO an interface or class? What is the method you're trying to mock?

Steve Freeman
its a class I solved the problem
Gandalf StormCrow
+1  A: 

This and this posts describe how and why to test DAOs. (you can easily isolate spring and maven from the example, if you are not using them - the point is using HSQLDB)

Bozho
With mocks, the downside is that you have to implement potentially complex DAO/JPA/Database behavior (e.g., a store causes a private @Id field to become set). But, with a mock, you can generate error responses that would be perhaps difficult to create with staged data. Also, with a mocking framework, you keep the data for the test local to the test itself, not in a separate file. Is this correct? How do you weight this trade-off?
Emil
I think it's rarely justified to mock the dao - you need to have a very complex mock.
Bozho