File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Testing and the fly likes mocking deep down with mockito Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "mocking deep down with mockito" Watch "mocking deep down with mockito" New topic

mocking deep down with mockito

Wendy Gibbons

Joined: Oct 21, 2008
Posts: 1107

How do I write a test where the class I need to mock isn't static, and is deep down in the bowls of my application?

sorry I haven't got very far at all yet in my research.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

This question is hard to answer without an example. On a high level, you abstract something or subclass or ...

Can you provide an example?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Wendy Gibbons

Joined: Oct 21, 2008
Posts: 1107

Ther problem is that all of our code is called by reflection, with intefaces decided years ago. So nothing can be passed in the constructors.
Yesterday I was trying to mock the ElementTemplateMasterService to return a new response. But with this mornings refactor I am only trying to maock the FlexifieldMaster, but it is still an object I have no control over the instantiation of.

This is the test I am trying to fix (currently in progress trying to sort out an answer):

this is the method I am trying to stub out (on FlexiFieldMaster)
Wendy Gibbons

Joined: Oct 21, 2008
Posts: 1107

well with help I got the previous version to work using doReturn, but then other code after the call caused problems, so I had to stub even higher up and endud up using doNothing.

So my question has become, Is there any good documentation that actually documents this without presuming you are a master of easyMock just converting?

I agree. Here's the link:
subject: mocking deep down with mockito
It's not a secret anymore!