这是我的代码 -
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.core.classloader.annotations.*;
import static org.powermock.api.support.SuppressCode.*;
class BaseService {
public int save() {
validate();
return 2;
}
public static int save2() {
return 5;
}
public void validate() {
System.out.println("base service save executing...");
}
}
class ChildService extends BaseService {
public int save() {
System.out.println("child service save executing...");
int x = super.save2();
int y = super.save();
System.out.println("super.save returned " + y);
load();
return 1 + x;
}
public void load() {
System.out.println("child service load executing...");
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(BaseService.class)
public class PreventSuperInvocation {
@Test
public void testSave() throws Exception {
org.powermock.api.support.Stubber.stubMethod(BaseService.class,
"save2", 4);
suppressMethod(BaseService.class, "save");
ChildService childService = new ChildService();
System.out.println(childService.save());
}
}
我想super.save()
在ChildService
课堂上嘲讽。但我找不到这样做的方法。suppressMethod()
仅抑制并返回默认值(在上述情况下为 0)。像MemberModifier
,Stubber
之类的东西MethodProxy
只适用于静态方法。
有没有办法在 Powermock 中做到这一点?
我正在使用 Powermock 1.5 和 Mockito 1.9.5。