Mocking static method of the interface in Java

Viewed 1230

I got an interface with a static method in it. And I want to mock this method for unit tests. Here's an example of such an interface:

public interface IClass {
  static String create(String s) {
    System.out.println("Method create is called");
    return s;
  }
}

I was trying to mock this method using PowerMockito:

@RunWith(PowerMockRunner.class)
@PrepareForTest(IClass.class)
public class IClassTest {

  @Before
  public void setUp() {
    PowerMockito.mockStatic(IClass.class);
    ClassImpl cl = mock(ClassImpl.class);
    Mockito.when(IClass.create(any())).thenReturn(cl);
  }

  @Test
  public void mockInterfaceClassTest() {
    System.out.println(IClass.create("Test"));
  }
}

Unfortunately, the mocking is not being done and even more, as soon as in my setUp() method I'm trying to setup mock: Mockito.when(IClass.create(any())).thenReturn(cl); actually, the method is being called (I got message in console "Method create is called"), which is undesired for sure.

Is there any possibility to mock static method inside interface?

0 Answers
Related