Mock Static Methods in JUnit5 using PowerMockito

Viewed 61025

Need help for Mocking Static methods using JUnit5 with PowerMockito framework.

Powermock junit5 and mockito2.x not working RunnerTestSuiteChunker not found

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

import static org.mockito.Mockito.*;


@PrepareForTest(EnvironmentUtils.class)
@RunWith(PowerMockRunner.class)
public class RuleControllerTest {       

        @Before
        public void setup() {           
            PowerMockito.mockStatic(EnvironmentUtils.class);
        }   


        @Test
        public void test_rule_create_rule() throws IOException {
            when(EnvironmentUtils.isCF()).thenReturn(true);

        }
}

and pom.xml

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.23.4</version>
    <scope>test</scope>
</dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>

I followed Junit5 sample from here, 1) https://www.baeldung.com/junit-5 2) Junit5 mock a static method

But facing an issue as, I know there is an existing issue for Junit5 with powermock but any one knows any other way to Mock static methods using JUnit5 using powermock.

3 Answers

You're using @Before which is junit4 annotation.. Junit5 have @BeforeEach / @BeforeAll(whichever suits your requirement). Also, your import for @Test are from junit4 but not from junit5(That should be org.junit.jupiter.api.Test;)

As your link suggests, still you can't do power mock stuff with junit-5 directly, simply because there's no PowerMockRunner (Extension) still available for junit-5.

However, In your above code possibly what has gone wrong is this line.

when(EnvironmentUtils.isCF()).thenReturn(true);

Here, note that you are using when of mockito (by import static org.mockito.Mockito.*;)

Instead, you have to use the one of PowerMockito. So

Remove this line import static org.mockito.Mockito.*;

Instead, add this. import static org.powermock.api.mockito.PowerMockito.*;

Related