Replacing inherited final methods with jmockit
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 a开发者_开发技巧long 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?
Found a solution...looks like I just had to mock the base class and the derived instances also got updated:
...
new MockUp<Base>() {
@Mock int getX() { return 10;}
};
Derived d = new Derived();
System.out.println(plot.getWidth()); // prints 10
...
精彩评论