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 
UnexpectedExceptionTest1.java

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

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

(Continued on next question...)

Other Interview Questions