Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do you configure PowerMock and Mockito in a Maven project


How do you configure PowerMock and Mockito in a Maven project


Configuring PowerMock and Mockito in a Maven project involves several steps, including adding the necessary dependencies and setting up your test classes. Here's a detailed guide on how to do it:

Step 1: Add Dependencies to `pom.xml`

To use PowerMock with Mockito in a Maven project, you need to add the following dependencies to your `pom.xml` file. Ensure you are using the correct version of PowerMock that supports your Mockito version.

xml

    
    
        org.mockito
        mockito-core
        2.19.0 
        test
    

    
    
        junit
        junit
        4.12
        test
    

    
    
        org.powermock
        powermock-api-mockito2
        2.0.0-beta.5 
        test
    

    
    
        org.powermock
        powermock-module-junit4
        2.0.0-beta.5 
        test
    

Step 2: Configure Maven Compiler and Surefire Plugins

Ensure that your Maven project uses the correct Java version and configure the Maven Surefire plugin to run your tests. Here's how you can do it:

xml

    
        
            maven-compiler-plugin
            3.7.0
            
                10
                10
            
        
        
            org.apache.maven.plugins
            maven-surefire-plugin
            2.22.0
            
                
                    org.apache.maven.surefire
                    surefire-junit47
                    2.22.0
                
            
        
    

Step 3: Write Test Classes

To test static methods using PowerMock and Mockito, you need to annotate your test class with `@RunWith(PowerMockRunner.class)` and `@PrepareForTest` to specify the class containing the static method you want to mock.

Here's an example of how to write a test class for a static method:

java
package com.example.mockito.staticmethod;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Utils.class)
public class JUnit4PowerMockitoStaticTest {

    @Test
    public void test_static_mock_methods() {
        PowerMockito.mockStatic(Utils.class);
        
        PowerMockito.when(Utils.print("Hello")).thenReturn(true);
        PowerMockito.when(Utils.print("Wrong Message")).thenReturn(false);
        
        assertTrue(Utils.print("Hello"));
        assertFalse(Utils.print("Wrong Message"));
        
        PowerMockito.verifyStatic(Utils.class, Mockito.atLeast(2));
        Utils.print(ArgumentMatchers.anyString());
    }
}

Step 4: Run Tests

After setting up your test classes, you can run your tests using Maven. Ensure that your test classes are correctly placed in the `src/test/java` directory.

You can run your tests using the following Maven command:

bash
mvn clean test

Note on JUnit 5 Support

As of now, PowerMock does not support JUnit 5. If you are using JUnit 5, you might need to consider using Mockito's built-in support for mocking static methods, which was introduced in Mockito 3.4.0. However, this requires a different setup and does not involve PowerMock[3].

Alternative: Replacing PowerMock with Mockito

If you're using a recent version of Mockito (3.4.0 or later), you can directly use Mockito's `mockStatic` method without needing PowerMock. This approach simplifies your setup and avoids the need for PowerMock altogether[3][9].

To migrate from PowerMock to Mockito for static method mocking, you can use tools like OpenRewrite to automate the process[3][9].

Citations:
[1] https://www.digitalocean.com/community/tutorials/mockito-mock-static-method-powermock
[2] https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito
[3] https://docs.openrewrite.org/recipes/java/testing/mockito/powermockitomockstatictomockito
[4] https://github.com/powermock/powermock/wiki/mockito
[5] https://mvnrepository.com/artifact/org.powermock
[6] https://stackoverflow.com/questions/1796833/running-junit-powermock-with-mockito-through-powermockrunner-from-maven
[7] https://groups.google.com/g/powermock/c/RDrznHUz6NQ
[8] https://stackoverflow.com/questions/58782253/what-dependency-to-add-in-maven-pom-for-using-powermock-with-mockito1
[9] https://docs.openrewrite.org/recipes/java/testing/mockito/replacepowermockito