在JMockit中调试部分模拟器 [英] Debug Partial Mock in JMockit

查看:210
本文介绍了在JMockit中调试部分模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JMockit 0.999.4和JDK6,是否可以调试为部分嘲笑的类?

Using JMockit 0.999.4 and JDK6, is it possible to debug into a partially mocked class?

请考虑以下测试:

@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;
    }
}

在SampleClass的每个sysout行上放置一个断点并且在Eclipse中调试Step Over将进入SampleClass方法。

Placing a breakpoint on each of the sysout lines in SampleClass and debug "Step Over" in Eclipse will enter the SampleClass methods.

考虑以下内容,这将阻止静态初始化程序将staticInt设置为10的值。 >

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.

是的,我尝试过-javaagent属性。

Yes, I have tried the -javaagent property.

推荐答案

p>由Rogerio在JMockit Google的讨论组中回答

Answered by Rogerio in the JMockit Google's discussion group.


JVM在重新定义
之后,丢弃在类上设置的断点(每当一​​个类被嘲笑时,它都是JMockit)。

The JVM discards the breakpoints set on a class after it is redefined (which JMockit does whenever a class is mocked).

要重置断点,请在测试方法之前停止调试器,只需
才能进入测试中的代码。也就是说,在
测试方法中设置一个断点,在这个
示例中调用SampleClass的行。

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.

这篇关于在JMockit中调试部分模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆