Using JMockit 0.999.4 and JDK6, is it possible to debug into a partially mocked class?
Consider the following test:
@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;
}
}
Placing a breakpoint on each of the sysout lines in SampleClass and debug "Step Over" in Eclipse will enter the SampleClass methods.
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.
Yes, I have tried the -javaagent property.
Answered by Rogerio in the JMockit Google's discussion group.
The JVM discards the breakpoints set on a class after it is redefined (which JMockit does whenever a class is mocked).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With