欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot单元测试如何使用 mock静态方法

程序员文章站 2022-10-03 14:22:40
The article summary1 为什么要对静态方法mock2 如何使用`powermock`对静态方法`mock`2.1 添加依赖2.2 编写测试类2.3 需要注意的问题1 为什么要对静态方法mockmockito库并不能 mock静态方法,需要依赖powermock才能对静态方法mock2 如何使用powermock对静态方法mock2.1 添加依赖顺便添加对JsonPath的依赖



1 为什么要对静态方法mock

mockito库并不能 mock静态方法,需要依赖powermock才能对静态方法mock

2 如何使用powermock对静态方法mock

2.1 添加依赖

顺便添加对JsonPath的依赖

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <properties> <powermock.version>1.7.0RC4</powermock.version> <mockito.version>2.7.5</mockito.version> </properties> <dependencies> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>2.4.0</version> <scope>test</scope> </dependency> <!-- powermock --> <!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito --> <dependency><!-- 如果不是springboot,不需要加这个 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <exclusions> <exclusion> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> </exclusion> </exclusions> <scope>test</scope> </dependency> <!-- powermock --> <!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito --> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>${powermock.version}</version> <exclusions> <exclusion> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> </exclusion> </exclusions> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 --> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4-rule --> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4-rule</artifactId> <version>${powermock.version}</version> <exclusions> <exclusion> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> </exclusion> </exclusions> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core --> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>${mockito.version}</version><!--$NO-MVN-MAN-VER$--> <scope>test</scope> </dependency> </dependencies> <build> <finalName>nef-server</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 

2.2 编写测试类

前提:junit用的是4.12
工具类

public class IDUtil { public static String getSubscriptionId() { String localhost = NetUtil.getLocalhostStr(); log.info("获取到的本地URL是:{}", localhost); return IdUtil.getSnowflake(Math.abs(localhost.hashCode()) % 32, Math.abs(localhost.hashCode()) % 32).nextIdStr(); } } 

测试类

/**
 * <p>事件订阅测试类</p>
 *
 * @Author: lid
 * @Date: 2020-08-19
 */ @RunWith(PowerMockRunner.class) // 步骤1 @PowerMockRunnerDelegate(SpringRunner.class)// 步骤2 @PowerMockIgnore( {"javax.management.*", "javax.net.*"})// 步骤3 @PrepareForTest(IDUtil.class)// 步骤4:指定提前mock哪个类 @SpringBootTest() public class HelloTest { @Autowired(required = true) private WebApplicationContext webApplicationContext; @Autowired(required = false) private MockMvc mockMvc; @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); //步骤5 PowerMockito.mockStatic(IDUtil.class); } @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); PowerMockito.mockStatic(IDUtil.class);// 步骤6 } } 

测试结果

打印结果:
hello

2.3 需要注意的问题

有时候单个方法测试可以通过,但是整个测试类一直运行时通不过,powermockmockito版本适配如下:
SpringBoot单元测试如何使用 mock静态方法

本文地址:https://blog.csdn.net/lida776537387/article/details/108246006