赞
踩
PowerMockito可以用来mock静态方法
目录结构:
pom文件:
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.da</groupId> <artifactId>java-test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>java-test</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.17.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <mockito.version>2.8.9</mockito.version> <powermock.version>1.7.4</powermock.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- powermock 的依赖 --> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>${mockito.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
测试类:
package com.da.javatest; import java.util.Date; import com.da.javatest.demo.OvertimeUtil; import com.da.javatest.service.MySecondService; import com.da.javatest.service.MyService; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; 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 org.powermock.modules.junit4.PowerMockRunnerDelegate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(SpringRunner.class) // 这里 Ignore的类都会通过spring的ClassLoader来加载,尽量要让类都从spring的ClassLoader来加载,否则有问题 @PowerMockIgnore({ "com.*", "io.*", "org.*", "ch.*", "javax.validation.*" }) @PrepareForTest({OvertimeUtil.class}) @SpringBootTest(classes = { Application.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class JavaTestApplicationTests { @Autowired private MyService myService; @Mock private MySecondService mySecondService; @Test public void contextLoads() { PowerMockito.mockStatic(OvertimeUtil.class); Mockito.when(OvertimeUtil.calculationTime(Mockito.any(), Mockito.any())).thenReturn("4d"); System.out.println(OvertimeUtil.calculationTime(new Date(), new Date())); System.out.println(myService.sayHello("Da.")); Mockito.when(mySecondService.show(Mockito.anyString())).thenReturn("show test!"); System.out.println(mySecondService.show("asd")); } }
启动类:
package com.da.javatest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
接口1:
package com.da.javatest.service;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String sayHello(String name) {
return "hello " + name;
}
}
接口2:
package com.da.javatest.service;
import org.springframework.stereotype.Service;
@Service
public class MySecondService {
public String show(String text) {
return text;
}
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。