Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock Static Methods in JUnit5 using PowerMockito

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.

like image 379
chaitanya Avatar asked May 23 '20 16:05

chaitanya


People also ask

How do you mock a static method in PowerMockito?

Use PowerMockito. mockStatic() for mocking class with static methods. Use PowerMockito. verifyStatic() for verifying mocked methods using Mockito.

Can we mock static methods in junit5?

With some overhead you can: As JUnit 5 provides support for running legacy JUnit 4, and there you can use Mockito. So you can create tests in Junit4 for these cases: A sample project for migration setup with gradle and with mvn. From there I am using PowerMock 2.0 beta with Mockito 2.

Can we mock static methods using MOQ?

You can use Moq to mock non-static methods but it cannot be used to mock static methods.

How do you mock a static method in jest?

To mock the static method, simply assign jest. fn() to the static method like so: Another scenario is when private constructor is used to instantiate a class (see: When to use a private constructor), the question arises — how do I mock the dependencies?


1 Answers

With mockito v 3.4.0 we can simply use mockStatic() method without powermock library :

 try (MockedStatic mocked = mockStatic(Foo.class)) {
   mocked.when(Foo::method).thenReturn("bar");
   assertEquals("bar", Foo.method());
   mocked.verify(Foo::method);
 }

assertEquals("foo", Foo.method());

Latest documentation and example : https://javadoc.io/static/org.mockito/mockito-core/3.5.10/org/mockito/Mockito.html#static_mocks

like image 152
Tomasz Avatar answered Oct 05 '22 01:10

Tomasz