java powermock公共桩_java-Powermock-模拟超级方法调用

这是我的代码-

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());

}

}

我想在ChildService类中模拟super.save().但是我找不到做到这一点的方法. preventMethod()仅禁止并返回默认值(上述情况下为0).诸如MemberModifier,Stubber,MethodProxy之类的东西仅适用于静态方法.

有没有办法在Powermock中做到这一点?

我正在使用Powermock 1.5和Mockito 1.9.5.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值