Skip to content
Advertisement

How to write or mock interface methods available in jar?

Having Method like

public boolean getConfirmation(int timeout) {
  Selection Selection;
 try {                                                                            
      Selection = XYZ.getHelperCommands().getDisplayConfirmation(timeout);               
      } catch (Exception e) {                                                                   
         return false;                                                                               
       }                                                                                       
        boolean result=false;
    if(Selection!=null) {
        result= (Selection.compareTo(Selection.YES) == 0);
    } 
    logger.info("getConfirmation() completed with result : " + result);
    return result ;
}

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

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>${junit.vintage.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.0.0</version>
        </dependency>

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…

public class HelperCommandWrapper {
   public Selection getDisplayConfirmation() {
     return XYZ.getHelperCommands().getDisplayConfirmation(timeout);
   }
}

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…

public WhateverYourClassNameIs(HelperCommandWrapper helperCommandWrapper) {
    this.helperCommandWrapper = helperCommandWrapper;
}

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

public boolean getConfirmation(int timeout) {
  Selection Selection;
 try {                                                                            
      Selection = this.helperCommandWrapper.getDisplayConfirmation(timeout);               
      } catch (Exception e) {                                                                   
         return false;                                                                               
       }        

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

Advertisement