• 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

Query regariding Mockito

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 40
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic