使用PowerMockito模拟JUnit5中的静态方法

问题描述 投票:0回答:3

需要使用带有PowerMockito框架的JUnit5模拟静态方法的帮助。

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

        }
}

和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-52)Junit5 mock a static method

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

java junit5 powermockito junit5-extension-model
3个回答
0
投票

正如您的链接所暗示的,仅由于junit-5仍然有no] [C0(扩展名),您仍然无法直接使用junit-5进行模拟操作。

但是,在您的上面的代码中,可能这行错了。

PowerMockRunner

这里,请注意,您正在使用when(EnvironmentUtils.isCF()).thenReturn(true); when(按mockito

相反,您必须使用import static org.mockito.Mockito.*;之一。所以

删除此行PowerMockito

相反,添加它。 import static org.mockito.Mockito.*;


0
投票

删除静态导入


0
投票

我重新激活此线程只是想知道您是否找到解决问题的方法/解决方法?

© www.soinside.com 2019 - 2024. All rights reserved.