Skip to content
Advertisement

How to write or mock interface methods available in jar?

Having Method like

JavaScript

in above method helperCommands is a interface in my Jar file that contains getDisplayConfirmation() method my question is how can i mock this method i check below link but no help

Unit testing of Jar methods in java i’m using below dependency

JavaScript

is it mendatory to use powerMockRunner ? or above code is enough to write junit?

Advertisement

Answer

I am asuming that XYZgetHelperCommands() is some static call. In this case, I would suggest not using static mocking, but instead using a wrapper plus dependency injection. In other words, first you create a simple class…

JavaScript

So, now you have a class that you can mock (ideally, use an interface). And now you simply give an instrance of that class into the constructor of your class…

JavaScript

…and now you can use that in your code and also easily mock it…

JavaScript

And voila, now you can easily mock your specific usecase without having to care that the original implementation will call a static method.

Advertisement