使用PowerMockito模拟JUnit5中的静态方法 [英] Mock Static Methods in JUnit5 using PowerMockito

查看:377
本文介绍了使用PowerMockito模拟JUnit5中的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要在PowerMockito框架中使用JUnit5模拟静态方法的帮助.

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

​​ Powermock junit5和mockito2.x无法正常运行RunnerTestSuiteChunker找不到

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);

        }
}

和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>

我从这里关注了Junit5示例, 1) https://www.baeldung.com/junit-5 2) Junit5模拟静态方法

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

但是面对一个问题,我知道Junit5有一个现有的 issue 使用powermock,但是任何人都知道使用powermock使用JUnit5模拟静态方法的任何其他方式.

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.

推荐答案

使用Mockito v 3.4.0,我们可以简单地使用 mockStatic()方法,而无需使用powermock库:

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());

最新文档和示例: https://javadoc. io/static/org.mockito/mockito-core/3.5.10/org/mockito/Mockito.html#static_mocks

这篇关于使用PowerMockito模拟JUnit5中的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆