This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query regariding Mockito

 
bs vinay
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tomek,
I am presently using a combination of TestNG and mockito for unit testing.

I have a question in the Mockito. Is there any way to mock the static utility classes in the Mockito?
For example, if i need to Mock a factory method, what is the correct way to mock and is the PowerMock best here?

Please help me in this. Thanks.

Regards,
Vinay
 
Tomek Kaczanowski
author
Ranch Hand
Posts: 40
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinay,

bs vinay wrote:I have a question in the Mockito. Is there any way to mock the static utility classes in the Mockito?

No, Mockito does not allow you to do that. In short, mocking of static classes is considered to be signal of a bad (ok, suboptimal) design of your production code, and one should strive for something more loosely coupled (e.g. via Dependency Injection) which is easier to test. So, this is a design decision by Mockito author(s) and probably won't change.

bs vinay wrote:For example, if i need to Mock a factory method, what is the correct way to mock and is the PowerMock best here?

If what you really want it to mock a static method then PowerMock is a good solution. However, some would argue, that by doing so you are really ignoring an important information that Mockito gives you: namely, that your class is very tightly coupled to some other class. There is a popular saying that "if you can't write test, it means you have bad code". So, yes, you can use PowerMock, but you can also redesign your class and probably end up with better design (and then test it without problems using Mockito). I would encourage you to go with redesign, but I understand this is not always a viable option (e.g. working with legacy code, no time etc.)
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic