使用 PowerMockito 在 JUnit5 中模拟静态方法
2022-09-04 01:56:10
需要帮助使用JUnit5和PowerMockito框架模拟静态方法。
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模拟静态方法
但是面对一个问题,我知道 Junit5 有一个现有的问题,但任何人都知道任何其他方法来模拟使用 JUnit5 使用 powermock 的静态方法。