This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Testing and the fly likes Return String on a Mock method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Return String on a Mock method" Watch "Return String on a Mock method" New topic
Author

Return String on a Mock method

Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

I have the following method:




How can I mock this? Something inside this could work?

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

This looks like something in a Data Access Object (DAO). Do you have an interface defined for the DAO? Are you using a mocking framework like Mokito? These will make it a lot easier to mock out DAOs.


Junilu - [How to Ask Questions] [How to Answer Questions]
Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

Yes I am. I did this for other methods but I don't know how to return a String to this type of method. I'm learning mocking.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

It appears you are trying to hand-craft your DAO mocks. Over time this can get very tedious and difficult to maintain because not only will you have to maintain your production code and the tests for them, you'll also have to maintain your hand-crafted mocks. This also adds complexity that I don't think is really necessary.

If you define an interface for your DAOs and use a mocking framework like Mokito, you can have Mokito manage your mock DAOs for you. Then you can concentrate on the parts of the tests that you should be: the test data and the assertions that you want to make. Unit tests need to run very fast and they shouldn't cross any execution boundaries like having to read the file system or access a database or access a Web Service. I suggest you keep your test data/mock data in your test classes (this is in reference to another thread you started).
Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

But for example, for the following code:



I have the following mock:




How it could be for the other method?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

Is there anything wrong with just doing:

?

But my point is, why bother even doing that when you could do this instead:



So here, I'm concentrating on the things I need to do in line 18 and 20 - I don't have to worry about the rest of the stuff about the Mock, Mokito takes care of that for me.
Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

No, there's no problem. Well my objective with this is to learn how to do Mocks. That's why I'm not using any framework... Anyway if you saw the getList method that I showed in the previous post, I'm passing a "Integer[] ls" as a parameter. I'm doing this wrong because I'm not using it. How would you do this?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

If you want to learn how to use the mock objects technique properly, I suggest that you don't try to write hand-crafted Mocks because that's almost like saying "I want to learn how to drive a car" then going out to the hardware store to buy parts so you can build your own car. Way too much work and way too complicated.

I would suggest that you first read up on the motivation for doing mocking/stubbing. Then find a good mocking framework and learn how to use it. My current preferred framework is Mokito but there are many other mocking frameworks out there to choose from.

Good luck.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Daniel Afonso wrote:No, there's no problem. Well my objective with this is to learn how to do Mocks. ...

If this is the objective then reading a book like has been suggested is much better than starting many threads for the same problem in the forums.
Another mistake you might already have made is starting with some DAO that may or may not be testable.
If you wrote your test cases first you would have been forced to write testable implementations. This is a good thing because testable code is reusable code.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Return String on a Mock method
 
Similar Threads
PL/SQL method
Jenny JDBC Code Generation
better option JdbcTemplate.queryForList(query)
Resultset question
Help with FK in PreparedStatement