在JMockit中调试部分模拟器
使用JMockit 0.999.4和JDK6,是否可以调试为部分嘲笑的类?
Using JMockit 0.999.4 and JDK6, is it possible to debug into a partially mocked class?
请考虑以下测试:
@Test
public void testClass() {
SampleClass cls = new SampleClass();
System.out.println(cls.getStaticInt());
cls.setVal(25);
System.out.println(cls.getVal());
}
static class SampleClass {
static int staticInt = 5;
private int val;
{
staticInt = 10;
}
public int getStaticInt() {
System.out.println("Returning static int and adding a line for debugging");
return staticInt;
}
public void setVal(int num) {
System.out.println("Setting val and adding a line for debugging");
this.val = num;
}
public int getVal() {
System.out.println("Returning val and adding a line for debugging");
return this.val;
}
}
在SampleClass的每个sysout行上放置一个断点并且在Eclipse中调试Step Over将进入SampleClass方法。
Placing a breakpoint on each of the sysout lines in SampleClass and debug "Step Over" in Eclipse will enter the SampleClass methods.
考虑以下内容,这将阻止静态初始化程序将staticInt设置为10的值。 >
Consider the following which will prevent the static initializer from setting staticInt to a value of 10.
@Test
public void testClass(@Mocked(methods = "$clinit") SampleClass cls) {
System.out.println(cls.getStaticInt());
cls.setVal(25);
System.out.println(cls.getVal());
}
static class SampleClass {
static int staticInt = 5;
private int val;
{
staticInt = 10;
}
public int getStaticInt() {
System.out.println("Returning static int and adding a line for debugging");
return staticInt;
}
public void setVal(int num) {
System.out.println("Setting val and adding a line for debugging");
this.val = num;
}
public int getVal() {
System.out.println("Returning val and adding a line for debugging");
return this.val;
}
}
但是,这段代码不会调试到
However, this code will not debug into the methods in SampleClass.
是的,我尝试过-javaagent属性。
Yes, I have tried the -javaagent property.
p>由Rogerio在JMockit Google的讨论组中回答
Answered by Rogerio in the JMockit Google's discussion group.
JVM在重新定义
之后,丢弃在类上设置的断点(每当一个类被嘲笑时,它都是JMockit)。
The JVM discards the breakpoints set on a class after it is redefined (which JMockit does whenever a class is mocked).
要重置断点,请在测试方法之前停止调试器,只需
才能进入测试中的代码。也就是说,在
测试方法中设置一个断点,在这个
示例中调用SampleClass的行。
To reset the breakpoints, stop the debugger at the test method, just before it enters the code under test. That is, set a breakpoint in the test method, on the line which calls into "SampleClass" in this example.