• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Return String on a Mock method

 
Greenhorn
Posts: 16
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following method:




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

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Daniel Afonso
Greenhorn
Posts: 16
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But for example, for the following code:



I have the following mock:




How it could be for the other method?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic