测试一个public/protected函数
最基础的用法,现在你有一个类FuncService
class FuncService
{
public String funcA(final Integer value) {
if (userId == 0) {
return "root";
}
return "user";
}
protected String funcB(final Integer value) {
if (userId == 0) {
return "root";
}
return "user";
}
}
新建一个FuncServiceUnitTest.java的文件
@RunWith(PowerMockRunner.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@PrepareForTest({FuncService.class})
@PowerMockIgnore({
.... //省略
})
public class FuncServiceUnitTest {
@Spy
@InjectMocks
private FuncService funcService = PowerMockito.spy(new FuncService());
@Test
public void funcA_returnAdmin() {
//public method直接调用即可
Assertions.assertEquals("root