Question

I'm having problems debugging a JUnit testcase. I'm using Java v6 and JUnit Framework v3.8.2.

I invoke jdb like this:

jdb junit.textui.TestRunner MyTest

The problem is that I don't know the name of the object TestRunner creates from my TestCase class. 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:

[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. 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.

Was it helpful?

Solution

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. It works now.

OTHER TIPS

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?

This would probably be easiest with a visual debugger. It also assumes that the JUnit instance you're running was built with debugging support. You won't get any symbolic information otherwise.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top