Powermock - mocking a super method invocation(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()
.但我找不到这样做的方法.suppressMethod()
只抑制并返回一个默认值(在上述情况下为 0).而 MemberModifier
、Stubber
、MethodProxy
之类的东西只适用于静态方法.
I would like to mock super.save()
in ChildService
class. But I can't find a way of doing it. suppressMethod()
only suppresses and returns a default value (0 in the above case). And things like MemberModifier
, Stubber
, MethodProxy
only work for static methods.
有没有办法在 Powermock 中做到这一点?
Is there a way of doing this in Powermock?
我正在使用 Powermock 1.5 和 Mockito 1.9.5.
I'm using Powermock 1.5 and Mockito 1.9.5.
推荐答案
看来jMockit可以满足我的需求.也许我会将这个问题发布到 powermock 邮件列表.同时下面应该足够了.包 learning_mocking_tools.learning_mocking_tools;包 learning_mocking_tools.learning_mocking_tools;
It seems that jMockit can do what I need. Maybe I will I post this question to the powermock mailing list. Meanwhile below should suffice. package learning_mocking_tools.learning_mocking_tools; package learning_mocking_tools.learning_mocking_tools;
import mockit.*;
import org.junit.Assert;
import org.junit.Test;
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 + y;
}
public void load() {
System.out.println("child service load executing...");
}
}
@MockClass(realClass = BaseService.class)
class MockBase {
@Mock
public int save() {
System.out.println("mocked base");
return 9;
}
}
public class PreventSuperInvocation {
@Test
public void testSave() throws Exception {
MockBase mockBase = new MockBase();
Mockit.setUpMock(BaseService.class, mockBase);
ChildService childService = new ChildService();
// int x = childService.save();
Assert.assertEquals(9 + 1, childService.save());
Mockit.tearDownMocks();
}
}
这篇关于Powermock - 模拟一个超级方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Powermock - 模拟一个超级方法调用
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01