8

在使用 JUnit5 和 PowerMockito 框架的 Mocking Static 方法方面需要帮助。

Powermock junit5 和 mockito2.x 不工作 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-5 2)Junit5 模拟静态方法

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

4

3 回答 3

13

使用 mockito v 3.4.0 我们可以简单地使用没有 powermock 库的 mockStatic()方法:

 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

于 2020-09-16T13:21:05.760 回答
1

您正在使用 @Before ,这是 junit4 注释.. Junit5 有 @BeforeEach / @BeforeAll (以适合您的要求为准)。另外,您对@Test 的导入来自junit4,而不是来自junit5(应该是org.junit.jupiter.api.Test;)

于 2020-09-17T18:10:47.030 回答
0

正如您的链接所暗示的那样,您仍然不能直接使用 junit-5 进行电源模拟,只是因为junit-5 仍然没有 PowerMockRunner(扩展)可用。

但是,在您上面的代码中,可能出了问题的是这一行。

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

在这里,请注意您使用when的是mockito(by import static org.mockito.Mockito.*;)

相反,您必须使用PowerMockito. 所以

删除此行import static org.mockito.Mockito.*;

相反,添加这个。import static org.powermock.api.mockito.PowerMockito.*;

于 2020-05-25T14:20:48.123 回答