code: public static String getBacktrace(JUnitTestData testMethod) {
          StringBuilder stackTrace = new StringBuilder();
  
          Throwable throwable = testMethod.getFailException();
  
          if (throwable != null) {
              StringWriter sw = new StringWriter();
              PrintWriter pw = new PrintWriter(sw);
              throwable.printStackTrace(pw);
              String stackTraceString = sw.toString();
              stackTraceString = stackTraceString.trim().replaceAll("\\r\\n",
                      "\n");
  
              StringTokenizer st = new StringTokenizer(stackTraceString,
                      LINE_SEPARATOR);
  
              while (st.hasMoreTokens()) {
                  String stackTraceLine = st.nextToken();
                  stackTrace.append(stackTraceLine);
                  stackTrace.append(LINE_SEPARATOR);
              }
  
          } else {
              stackTrace.append('~');
          }
  
          return stackTrace.toString();
      }
docs: Get the backtrace from a given failed JUnit test result
  
  @param testMethod
  @return Backtrace from a given failed JUnit test result