用 jmockit 替换继承的最终方法
我正在尝试找到一种方法,用我自己使用 jMockIt 的实现来替换继承的最终方法。
假设我有以下内容:
public class Base {
...
public final int getX() {...}
}
public class Derived extends Base {
}
有没有办法可以重新定义 getX() 以始终返回 10?
我尝试按照以下方式做一些事情:
new Base() {
@Mock
public int getX() {
return 10;
}
};
Derived d= new Derived();
System.out.println(d.getX());
这会产生一个关于 jMockIt 无法找到 int getX() 的匹配方法的运行时异常。
我遇到了这个线程: http://groups.google.com/group/jmockit-users/browse_thread /thread/27a282ff2bd4ad96
但我不太明白那里提供的解决方案。
有人可以帮忙吗?
I'm trying to find a way to replace an inherited final method with my own implementation using jMockIt.
Lets say I have the following:
public class Base {
...
public final int getX() {...}
}
public class Derived extends Base {
}
Is there a way that I can redefine getX() to always return 10 for example?
I tried doing something along the lines of this:
new Base() {
@Mock
public int getX() {
return 10;
}
};
Derived d= new Derived();
System.out.println(d.getX());
Which yields a runtime exception about jMockIt not being able to find a matching method for int getX().
I came across this thread:
http://groups.google.com/group/jmockit-users/browse_thread/thread/27a282ff2bd4ad96
But I don't quite grasp the solution provided there.
Anybody able to help out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了一个解决方案...看起来我只需要模拟基类并且派生实例也得到更新:
Found a solution...looks like I just had to mock the base class and the derived instances also got updated: