版本不对容易冲突,powermock包1.7以上不支持最新的mockito,用如下版本就行
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.10.19'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
测试类上注解
@RunWith(PowerMockRunner.class) // 告诉JUnit使用PowerMockRunner进行测试
@PrepareForTest({RandomUtil.class}) // 所有需要测试的类列在此处,适用于模拟final类或有final, private, static, native方法的类
@PowerMockIgnore("javax.management.*") //为了解决使用powermock后,提示classloader错误
其他用法同Mockito,并增加了一些增强方法,如
PowerMockito.whenNew(RedisStoreHandler.class).withAnyArguments().thenReturn(redisStoreHandler);