Mocking a static method using powermockito

Bahij.Mik

I am testing some legacy code and would like to mock any calls to a static logger method: LoggerFact.getLogger(Class class, String MethodName), here is what I tried:

@RunWith(PowerMockRunner.class)
@PrepareForTest({LoggerFact.class, MyClass.class, Logger.class})
public class MyClassTest
{
    @Before
    public void prepare() throws Exception
    {
      Logger mockedLogger = Mockito.mock(Logger.class);
      PowerMockito.mockStatic(LoggerFact.class);
      PowerMockito.when(LoggerFact.getLogger(MyClass.class, "test"))
                  .thenReturn(mockedLogger);
    }

    //My tests
}

The class that I am testing:

public class MyClass
{
    public String methodToBeTested()
    {
      Logger logger = LoggerFact.getLogger(this.getClass(), "test");
      logger.info("This is a test");
      //some logic
      return "SUCCESS";
    }
}

But I am recieving this error when I do this from the prepare when():

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.

Did I miss something? I checked a lot of older posts about this issue, but nothing worked for me.

Michael Easter

Working example here. Tested with JDK 8 and JDK 11.

The following works for me. (I have found that the order of initialization can be crucial):

@RunWith(PowerMockRunner.class)
@PrepareForTest(LoggerFact.class)
public class MyClassTestCase {
    private MyClass myClass = null;

    @Before
    public void setUp() {
        PowerMockito.mockStatic(LoggerFact.class);
    }

    @Test
    public void testMethodToBeTested() {
        Logger mockLogger = Mockito.mock(Logger.class);

        PowerMockito.when(LoggerFact.getLogger(eq(MyClass.class),eq("test"))).thenReturn(mockLogger);
        myClass = new MyClass();

        // test
        myClass.methodToBeTested();

        verify(mockLogger, times(1)).info(eq("This is a test"));
    }
}

As requested, from build.gradle in the example link above, here are the versions of libraries:

dependencies {
    testImplementation 'junit:junit:4.13.1'
    testImplementation 'org.mockito:mockito-core:3.6.0'
    testImplementation 'org.powermock:powermock-api-mockito2:2.0.7'
    testImplementation 'org.powermock:powermock-module-junit4:2.0.7'
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mocking a private method in junit using Powermockito

PowerMockito throwing exception when mocking static method of an enum and using that enum object inside a switch statement

Mocking static class with powermockito

PowerMockito mocking static method fails when calling method on parameter

PowerMockito: got InvalidUseOfMatchersException when use matchers mocking static method

Mocking static functions from other classes in Java using PowerMockito

Exception while using PowerMockito with static method

Verify Static Method Call using PowerMockito 1.6

MissingMethodInvocationException when using PowerMockito to test static method

Mocking static method using power mockito

Mocking private method call using PowerMockito returns null instead of returning List<String>: (Want not to execute private method)

Mocking method for all instances of class with PowerMockito

Verify private static method on final class gets called using PowerMockito

Unable to mock System class static method using PowerMockito

Mocking static Liferay method

jmock mocking a static method

Mocking static method

mocking UrlEncoder in a static method

PowerMockito mock indirect static method

Unit Tests, mocking static method using Context as variable in Kotlin with PowerMock

Mocking private method with PowerMockito only works starting with second method call

Mocking static method inside static method on powermock

PowerMockito: mocking private method and get a value without accessing it

Getting PowerMockito to mock a static method on an Interface?

Mocking local object creation of a final class using Powermockito

Content type error when mocking static method

Static method and constructor interception mocking in Scala/Spark

mocking a static Spring boot controller method with powermokito

UnexpectedInvocation while mocking a static method call