调试用Spock和JMockit测试的Java代码 [英] Debugging Java code tested with Spock and JMockit
问题描述
我正在使用Spock为Java应用程序编写测试。该测试包括该类的JMockit MockUp
。在调试测试时(使用IntelliJ),我希望能够进入Java代码。我意识到使用 F5
来进入代码将不起作用,因为所有的Groovy魔法都会反射出去。不幸的是,即使我在Java代码中设置了断点,它仍然不会被触发,即使它运行在代码中。
以下是要测试的代码:
public class TestClass {
private static void initializeProperties(){
// Code在测试期间不会运行
}
public static boolean getValue(){
return true; //< === BREAK POINT SET此处永远不会是HIT
}
}
以下是测试代码:
class TestClassSpec extends Specification {
void setup( ){
new MockUp< TestClass>(){
@Mock
public static void initializeProperties(){}
}
}
voidrun test(){
when:
boolean result = TestClass.getValue()
then:
result == true
}
}
测试通过并且一切正常,但我无法调试到Java代码。
$ b
注意:如果我不使用JMockit MockUp
,但是在我的生产代码中测试 private static
方法是必需的。
让调试器停止在使用JMockit的Spock测试中的Java代码中?
这个答案困扰了我好几天,而且我搜索并搜索了一个答案。在发布求助之后15分钟,我找到了答案。因此,对于遇到此问题的任何人,这里是我的解决方案 - 这一切都要归功于此答案: https://stackoverflow.com/a / 4852727/2601060
简而言之,当JMockit重新定义类时,将删除断点。解决方法是在测试中设置一个断点,一旦调试器停止在测试中,那么在Java代码中设置断点。
<有很多欢乐......
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?
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...
这篇关于调试用Spock和JMockit测试的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!