Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerMockito.verifyStatic() Problems

I need to use PowerMockito to test if a specific static method is called. I am using the following PowerMockito and JUnit libraries ...

  • powermock-mockito-1.6.2-full.jar
  • junit-4.12.jar

I am having issues getting the PowerMockito.verifyStatic() method to work properly. In the following code example, I have tried using the @PrepareForTest, and mockStatic(), and I have tried excluding them. In the code example I include them.

Test Class:

import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Test1.class)
public class PowerMockTest {
    @Test
    public void staticVerifyTest() {
        PowerMockito.mockStatic(Test1.class);

        // Test
        PowerMockito.verifyStatic();
        //Test1.staticMethod();
    }
}

Class Under Test:

public class Test1 {
    public static void staticMethod() {
        System.out.println("Static Method!");
    }
}

The test passes when it is run, but it should fail because Test1.staticMethod() is never called. Any help on this would be greatly appreciated!

like image 929
John A Qualls Avatar asked Oct 06 '15 18:10

John A Qualls


People also ask

Should we use PowerMockito?

Why use it? Powermock allows mocking static or final classes that can not be mocked in Mockito. It sounds great! It can mock anything that might not be the correct way of accessing objects in Java.

What is PowerMockito used for?

PowerMockito is a PowerMock's extension API to support Mockito. It provides capabilities to work with the Java Reflection API in a simple way to overcome the problems of Mockito, such as the lack of ability to mock final, static or private methods.

How do you verify a static method is called in Mockito?

To define mock behavior and to verify static method invocations, use the MockedStatic reference returned from the Mockito. mockStatic() method. It is necessary to call ScopedMock. close() method to release the static mock once it has been used and is no longer needed.


2 Answers

It's not an answer in itself but A) a confirmation that Oliver's comment about the method change is still valid at PowerMock 2.0.2 and B) a note with additional information on how it works.

PowerMockito.verifyStatic() calls Mockito.verify() which has a few examples at its Javadoc:

verify(mock, times(5)).someMethod("was called five times");
verify(mock, atLeast(2)).someMethod("was called at least two times");

As this sintax is not available anymore we need 2 lines of code to declare the validation rule. Using John's example this means that the first 3 lines would be "actual" business calls and the one after verifyStatic is just telling it which call counter must match the 2nd parameter:

PowerMockito.mockStatic(Test1.class);

// Test
Test1.staticMethod();
Test1.staticMethod();
Test1.staticMethod();

// Validation
PowerMockito.verifyStatic(Test1.class, Mockito.times(3));
Test1.staticMethod();
like image 106
Fabio Avatar answered Sep 21 '22 23:09

Fabio


Alright, I figured it out thanks to Stefan Birkner's reference

Here is the correction to my sample code:

import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Test1.class)
public class PowerMockTest {
    @Test
    public void staticVerifyTest() {
        PowerMockito.mockStatic(Test1.class);
        // Test
        Test1.staticMethod();
        PowerMockito.verifyStatic();
        Test1.staticMethod();
    }
}

After the static method is invoked, you need to verify that it was called by calling it again after your verifyStatic() call.

i.e.

        Test1.staticMethod();
        PowerMockito.verifyStatic();
        Test1.staticMethod();

You can also check if it was called multiple times like this...

Test1.staticMethod();
Test1.staticMethod();
Test1.staticMethod();
PowerMockito.verifyStatic(Mockito.times(3));
Test1.staticMethod();
like image 24
John A Qualls Avatar answered Sep 21 '22 23:09

John A Qualls