5

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.

ctran
  • 121
  • 1
  • 5

1 Answers1

7

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.

Bk Baba
  • 109
  • 1
  • 10
ctran
  • 121
  • 1
  • 5
  • For the curious, this is the JVM bug link: https://bugs.openjdk.java.net/browse/JDK-7036136 – The Alchemist Jun 26 '14 at 21:15
  • I can't even set a breakpoint directly on the JUnit test method. I'm using jdk 1.8 and Eclipse Mars (4.5.1). Does anyone else have the same problem and a solution to it? – Joern May 03 '16 at 13:44
  • Now I found the difference to the above test. I'm using the annotation `@Tested` on a member method. If I replace this by creating the SUT manually in a `@Before` method the breakpoints in the test class are working correctly. – Joern May 03 '16 at 14:05