Mockito无法对静态方法进行Mock,如果需要Mock静态方法,需要使用到PowerMockito。
1. Maven依赖
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.1</version>
</dependency>
2. 单元测试
单元测试时,需要使用PowerMockRunner及PrepareForTest两个注解:
@RunWith(PowerMockRunner.class)
@PrepareForTest({SysCache.