java - JMockit:如何在使用@Tested 注释时调试测试?

标签 java unit-testing debugging jmockit

问题Debug Partial Mock in JMockitDebugging Java code tested with Spock and JMockit已经解决了这个问题,当类被 JMockit 重新定义/检测时,被测软件 (SUT) 中的断点将被忽略。 推荐的解决方案是,您应该在测试类中添加一个额外的断点,以便在测试类中停止执行后重新激活 SUT 中的断点。

但是,如果您在测试类中使用 @Tested 注释,则此解决方案不起作用,因为在这种情况下,测试类本身的断点将被忽略。
这是一个例子:

package de.playground;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import mockit.Expectations;
import mockit.Injectable;
import mockit.integration.junit4.JMockit;

@RunWith(JMockit.class)
public class DebuggingWithJMockitTest {

    public interface Collaborator {
        String execute(String... args);
    }

    public static class ToTest {
        private Collaborator collaborator;

        public ToTest(Collaborator collaborator) {
            this.collaborator = collaborator;
        }

        public String doSomething() {
            return collaborator.execute("a", "b");
        }
    }


    @Injectable
    private Collaborator collaborator;

    @Tested
    private ToTest toTest;    

    @Test
    public void testHoldOnBreakpoint() {
        new Expectations() {{
                collaborator.execute((String[]) any); result = "whatever";
            }};

        String result = toTest.doSomething(); // add breakpoint here
        assertThat(result, is("whatever"));
    }
}

在这种情况下,调试器不会String result = toTest.doSomething(); 行停止。如果您不使用 @Tested 注释并在 @Before 方法中初始化 SUT,如下所示:

    // @Tested is not used
    private ToTest toTest;

    @Before
    public void before() {
        toTest = new ToTest(collaborator);
    }

断点工作得很好。

即使您在测试类中使用了 @Tested 注释,是否有任何解决方法可以调试您的代码?

最佳答案

这个错误 was brought up在 JMockit Google Group 上:

Yes, the problem is known, and already solved in JMockit 1.24.

它似乎没有记录问题。我们的团队在 JMockit 1.23 上遇到了这个问题,并且确实能够通过升级到 JMockit 1.24 来克服它。

关于java - JMockit:如何在使用@Tested 注释时调试测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37025602/

相关文章:

.net - 没有单元测试 (TDD) 的 QA 能否高效?

java - Camel 路线测试: Could not find a suitable setter for property:

c# - 枚举创建断点

debugging - Cheat Engine vs Ollydbg

c - 调试 C 时 Visual Studio 2010 挂起

java - long 不足以存储整数

java - 在Java中,如何删除Sqlite表

java - 如何为 setAsciiStream 方法提供正确的参数?

java - 将文本附加到 EditText 中字符串的中间

c# - 对 WCF 行为扩展进行单元测试