简体   繁体   English

如何使用jdb调试JUnit测试用例?

[英]How to debug a JUnit test case with jdb?

I'm having problems debugging a JUnit testcase. 我在调试JUnit测试用例时遇到问题。 I'm using Java v6 and JUnit Framework v3.8.2. 我正在使用Java v6和JUnit Framework v3.8.2。

I invoke jdb like this: 我这样调用jdb:

jdb junit.textui.TestRunner MyTest

The problem is that I don't know the name of the object TestRunner creates from my TestCase class. 问题是我不知道TestRunner从我的TestCase类创建的对象的名称。 For example, I'd like to print the return value of a method, but how do I call the method, when I don't know the name of the object? 例如,我想打印方法的返回值,但是当我不知道对象的名称时如何调用该方法?

Edit: The exception occurs at line 45, this is the call stack that I get when executing "where" in jdb: 编辑:异常发生在第45行,这是我在jdb中执行“ where”时得到的调用堆栈:

[1] MyTest.test1 (MyTest.java:44)
[2] sun.reflect.NativeMethodAccessorImpl.invoke0 (native method)
[3] sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:57)
[4] sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
[5] java.lang.reflect.Method.invoke (Method.java:616)
[6] junit.framework.TestCase.runTest (TestCase.java:164)
[7] junit.framework.TestCase.runBare (TestCase.java:130)
[8] junit.framework.TestResult$1.protect (TestResult.java:106)
[9] junit.framework.TestResult.runProtected (TestResult.java:124)
[10] junit.framework.TestResult.run (TestResult.java:109)
[11] junit.framework.TestCase.run (TestCase.java:120)
[12] junit.framework.TestSuite.runTest (TestSuite.java:230)
[13] junit.framework.TestSuite.run (TestSuite.java:225)
[14] junit.textui.TestRunner.doRun (TestRunner.java:121)
[15] junit.textui.TestRunner.start (TestRunner.java:185)
[16] junit.textui.TestRunner.main (TestRunner.java:143)

Edit2: I mixed things up, I know the name of the object I want to print a method of, but it still doesn't work. Edit2:我把事情弄混了,我知道要打印其方法的对象的名称,但是它仍然不起作用。 I wrote a small test case and class to explain the exact problem. 我写了一个小的测试用例和类来解释确切的问题。

This is the test case: 这是测试用例:

import junit.framework.TestCase;

public class TestJUnit extends TestCase {

    public void test1() {
        try {
            JUnitTestClass mtc = new JUnitTestClass();
            assertTrue(mtc.method1() == 1);
            assertTrue(mtc.method1() == 2);
        } catch (Exception exc) {
            fail(exc.getMessage());
        }
    }
}

And this is the class that is tested: 这是经过测试的类:

public class JUnitTestClass {

    private int att1 = 0;

    public int method1() {
        this.att1 += 1;
        return this.att1;
    }
}

Then I run the debugger like so: 然后我像这样运行调试器:

bash-4.2$ jdb -classpath $CLASSPATH junit.textui.TestRunner TestJUnit
Initializing jdb ...
> stop at TestJUnit:8
Deferring breakpoint TestJUnit:8.
It will be set after the class is loaded.
> run
run junit.textui.TestRunner TestJUnit
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
> 
VM Started: Set deferred breakpoint TestJUnit:8
.
Breakpoint hit: "thread=main", TestJUnit.test1(), line=8 bci=8
8                assertTrue(mtc.method1() == 1);

main[1] print mtc.method1()
com.sun.tools.example.debug.expr.ParseException: Name unknown: mtc.method1
mtc.method1() = null
main[1] dump mtc
com.sun.tools.example.debug.expr.ParseException: Name unknown: mtc
mtc = null

When I try to print the method or dump the object, I get "Name unknown". 当我尝试打印方法或转储对象时,出现“名称未知”。

Edit3: I simply forgot to use -g when using javac, sorry. Edit3:对不起,我在使用javac时只是忘记使用-g。

Sorry folks, I'm new to all of this and I made a silly mistake. 抱歉,我是新手,我犯了一个愚蠢的错误。 I forgot to use the -g command when calling javac to generate my test case class file. 我在调用javac生成测试用例类文件时忘记使用-g命令。 It works now. 现在可以使用了。

Can you set a breakpoint in one of your actual test methods, and then take a look at the call stack when it breaks, and step up into the actual JUnit code? 您可以在其中一种实际的测试方法中设置断点,然后在中断时查看调用堆栈,并逐步进入实际的JUnit代码吗?

This would probably be easiest with a visual debugger. 使用可视调试器可能最简单。 It also assumes that the JUnit instance you're running was built with debugging support. 它还假定您正在运行的JUnit实例是使用调试支持构建的。 You won't get any symbolic information otherwise. 否则,您将不会获得任何符号信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM