Search code examples
javajunitmockitoquarkuspowermockito

Can I use PowerMockito in Quarkus with JUnit5?


Im am currently building a Quarkus application and for unit testing I am using JUnit5. Currently I have no other option then using PowerMockito to mock my static functions but I cant seem to find the dependencies for it in my Quarkus project.

Does anyone know what the best set of dependecies should be used in a Quarkus app for PowerMockito with JUnit5?


Solution

  • I guess you don't need PowerMockito, just the capability to mock static methods. Since 2.7.x (released in 2017) Mockito community made an experimental library called mockito-inline for mocking static methods or final classes. Some features (e.g. static method mocking) have already merged into mockito-core. Additionally Quarkus has extension for Mockito.

    Here is a working example

    pom.xml

    <?xml version="1.0"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
             xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
        <groupId>io.github.zforgo</groupId>
        <artifactId>quarkus-mockito-static</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
            <compiler-plugin.version>3.8.1</compiler-plugin.version>
            <maven.compiler.release>11</maven.compiler.release>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
            <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
            <quarkus.platform.version>2.6.1.Final</quarkus.platform.version>
            <surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
        </properties>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>${quarkus.platform.group-id}</groupId>
                    <artifactId>${quarkus.platform.artifact-id}</artifactId>
                    <version>${quarkus.platform.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.quarkus</groupId>
                <artifactId>quarkus-arc</artifactId>
            </dependency>
            <dependency>
                <groupId>io.quarkus</groupId>
                <artifactId>quarkus-junit5</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>io.quarkus</groupId>
                <artifactId>quarkus-junit5-mockito</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>${quarkus.platform.group-id}</groupId>
                    <artifactId>quarkus-maven-plugin</artifactId>
                    <version>${quarkus.platform.version}</version>
                    <extensions>true</extensions>
                    <executions>
                        <execution>
                            <goals>
                                <goal>build</goal>
                                <goal>generate-code</goal>
                                <goal>generate-code-tests</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${compiler-plugin.version}</version>
                    <configuration>
                        <compilerArgs>
                            <arg>-parameters</arg>
                        </compilerArgs>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${surefire-plugin.version}</version>
                    <configuration>
                        <systemPropertyVariables>
                            <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                            <maven.home>${maven.home}</maven.home>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    Some utility class with static method

    package io.github.zforgo;
    
    public class SomeStaticClass {
    
        public static String getSome() {
            return "foo";
        }
    }
    

    Test class

    package io.github.zforgo;
    
    import io.quarkus.test.junit.QuarkusTest;
    import org.junit.jupiter.api.Test;
    import org.mockito.Mockito;
    
    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    @QuarkusTest
    public class SomeStaticClassTest {
    
        @Test
        void nonMocked() {
            assertEquals("foo", SomeStaticClass.getSome(), "Something went wrong");
        }
    
        @Test
        void mocked() {
            // try-with-resources is recommended in case of scoped (temporary) mocking 
            try (var mocked = Mockito.mockStatic(SomeStaticClass.class)) {
                mocked.when(SomeStaticClass::getSome).thenReturn("bar");
                assertEquals("bar", SomeStaticClass.getSome(), "Something went wrong");
            }
        }
    }