Interview Questions

Why Do I See "Unknown Source" in the Stack Trace of a Failed Test?

JUnit Questions and Answers

(Continued from previous question...)

Why Do I See "Unknown Source" in the Stack Trace of a Failed Test?

Normally, when a test fails, JUnit runner will dump the stack trace with the failing statement line number and all parent calling statement line numbers. The line numbers help you to locate those statements in Java source code files.

However, if a Java source code file is compiled with the "-g:none" (generate no debugging information) option, the failing statement will be included in the strack trace with "Unknow Source" as the number line.

To help you debug any code issue, you should always compile your test classes with the "-g" option to tell the compile to generate line numbers and other debugging information into the binary class code.

Here is a stack trace sample that shows you an example of "Unkown Source":

javac -g:none -cp .;junit-4.4.jar

java -cp .;junit-4.4.jar org.junit.runner.JUnitCore

JUnit version 4.4
Time: 0
There was 1 failure:
1) testGet(UnexpectedExceptionTest1)
java.lang.AssertionError: Unexpected exception
 at UnexpectedExceptionTest1.testGet(Unknown Source)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Tests run: 1,  Failures: 1

(Continued on next question...)

Other Interview Questions