1.pom文件添加maven依赖:
<properties>
<powermock.version>2.0.2</powermock.version>
</properties>
<!-- powermock -->
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<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>
<scope>test</scope>
</dependency>
2.单元测试,在测试类上加以下几个注解:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore( {"javax.management.*", "javax.net.*"})
@PrepareForTest(WebTTsUtil.class)
@SpringBootTest()
WebTTsUtil是我要mock的类,替换成你需要mock的类。
@Test
@Transactional
public void speechSynthesize() throws Exception {
PowerMockito.mockStatic(WebTTsUtil.class);
PowerMockito.when(WebTTsUtil.speechSynthesize(Mockito.anyString())).thenReturn(returnMap);
restReadExercisesMockMvc.perform(post("/api/speech/synthesis")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(map)))
.andExpect(status().isOk());
}
3.遇到的问题:
①单独测A测试类能通过,测试所有测试类A测试类通不过,可能是powermock和mockito的版本不适配,版本对应关系如下:
Mockito | PowerMock |
---|---|
2.8.9+ | 2.x |
2.8.0-2.8.9 | 1.7.x |
2.7.5 | 1.7.0RC4 |
2.4.0 | 1.7.0RC2 |
2.0.0-beta - 2.0.42-beta | 1.6.5-1.7.0RC |
1.10.8 - 1.10.x | 1.6.2 - 2.0 |
1.9.5-rc1 - 1.9.5 | 1.5.0 - 1.5.6 |
1.9.0-rc1 & 1.9.0 | 1.4.10 - 1.4.12 |
1.8.5 | 1.3.9 - 1.4.9 |
1.8.4 | 1.3.7 & 1.3.8 |
1.8.3 | 1.3.6 |
1.8.1 & 1.8.2 | 1.3.5 |
1.8 | 1.3 |
1.7 | 1.2.5 |
修改pom文件(junit用的是4.12):
<properties>
<powermock.version>1.7.0RC4</powermock.version>
<mockito.version>2.7.5</mockito.version>
</properties>
<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>
参考:1.https://blog.csdn.net/icarusliu/article/details/80429257;
2.http://ju.outofmemory.cn/entry/100348;
3.https://stackoverflow.com/questions/43940857/after-upgrade-to-2-7-classnotfoundexception-org-mockito-exceptions-reporter-whe;
4.http://stackmirror.caup.cn/page/sksdmpy4cfg7;
5.https://github.com/powermock/powermock/wiki/Mockito#supported-versions;
网友评论