2

I'm using Spock to write tests for a Java application. The test includes a JMockit MockUp for the class. When debugging a test (using IntelliJ) I would like to be able to step into the Java code. I realize using F5 to step into the code won't work, because of all the Groovy magic with reflection that goes on. Unfortunately, even if I set a breakpoint in the Java code, it still will not be hit, even though it runs through the code.

Here is the code to test:

public class TestClass {

    private static void initializeProperties() {
        // Code that will not run during test
    }

    public static boolean getValue() {
        return true;  // <=== BREAK POINT SET HERE WILL NEVER BE HIT
    }
}

Here is the test code:

class TestClassSpec extends Specification {

    void setup() {
        new MockUp<TestClass>() {
            @Mock
            public static void initializeProperties() {}
        }
    }

    void "run test"() {
        when:
        boolean result = TestClass.getValue()

        then:
        result == true
    }
}

The test passes and everything works well, but I'm not able to debug into the Java code.

Note: I can debug if I do not use the JMockit MockUp, but that is required to test a private static method in my production code.

Any ideas on how to get the debugger to stop in the Java code from a Spock test that uses JMockit?

mnd
  • 2,709
  • 3
  • 27
  • 48

1 Answers1

2

This answer has plagued me for days, and I have searched and searched for an answer. Fifteen minutes after posting for help, I find the answer . So for anyone else who runs into this, here's my solution - this is all thanks to this answer: https://stackoverflow.com/a/4852727/2601060

In short, the breakpoint is removed when the class is redefined by JMockit. The solution is to set a break point in the test, and once the debugger has stopped in the test THEN set the breakpoint in the Java code.

And there was much rejoicing...

Community
  • 1
  • 1
mnd
  • 2,709
  • 3
  • 27
  • 48
  • Unfortunately, this solution doesn't work for me. I'm using JMockit 1.23, jdk 1.8 and Eclipse Mars (4.5.1) and even setting a breakpoint in the JUnit test-class on the test-method does not convince the debugger to stop :( – Joern May 03 '16 at 13:52
  • 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