Example usage for java.lang AssertionError fillInStackTrace

List of usage examples for java.lang AssertionError fillInStackTrace

Introduction

In this page you can find the example usage for java.lang AssertionError fillInStackTrace.

Prototype

public synchronized Throwable fillInStackTrace() 

Source Link

Document

Fills in the execution stack trace.

Usage

From source file:com.gs.collections.impl.test.Verify.java

/**
 * Mangles the stack trace of {@link AssertionError} so that it looks like
 * its been thrown from the line that called to a custom assertion.
 * <p>// w w w .  j ava2  s  .  com
 * This is useful for when you are in a debugging session and you want to go to the source
 * of the problem in the test case quickly. The regular use case for this would be something
 * along the lines of:
 * <pre>
 * public class TestFoo extends junit.framework.TestCase
 * {
 *   public void testFoo() throws Exception
 *   {
 *     Foo foo = new Foo();
 *     ...
 *     assertFoo(foo);
 *   }
 *
 *   // Custom assert
 *   private static void assertFoo(Foo foo)
 *   {
 *     try
 *     {
 *       assertEquals(...);
 *       ...
 *       assertSame(...);
 *     }
 *     catch (AssertionFailedException e)
 *     {
 *       AssertUtils.throwMangledException(e, 2);
 *     }
 *   }
 * }
 * </pre>
 * <p>
 * Without the {@code try ... catch} block around lines 11-13 the stack trace following a test failure
 * would look a little like:
 * <p>
 * <pre>
 * java.lang.AssertionError: ...
 *  at TestFoo.assertFoo(TestFoo.java:11)
 *  at TestFoo.testFoo(TestFoo.java:5)
 *  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 *  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 *  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 *  at java.lang.reflect.Method.invoke(Method.java:324)
 *  ...
 * </pre>
 * <p>
 * Note that the source of the error isn't readily apparent as the first line in the stack trace
 * is the code within the custom assert. If we were debugging the failure we would be more interested
 * in the second line of the stack trace which shows us where in our tests the assert failed.
 * <p>
 * With the {@code try ... catch} block around lines 11-13 the stack trace would look like the
 * following:
 * <p>
 * <pre>
 * java.lang.AssertionError: ...
 *  at TestFoo.testFoo(TestFoo.java:5)
 *  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 *  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 *  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 *  at java.lang.reflect.Method.invoke(Method.java:324)
 *  ...
 * </pre>
 * <p>
 * Here the source of the error is more visible as we can instantly see that the testFoo test is
 * failing at line 5.
 *
 * @param e           The exception to mangle.
 * @param framesToPop The number of frames to remove from the stack trace.
 * @throws AssertionError that was given as an argument with its stack trace mangled.
 */
public static void throwMangledException(AssertionError e, int framesToPop) {
    e.fillInStackTrace();
    StackTraceElement[] stackTrace = e.getStackTrace();
    StackTraceElement[] newStackTrace = new StackTraceElement[stackTrace.length - framesToPop];
    System.arraycopy(stackTrace, framesToPop, newStackTrace, 0, newStackTrace.length);
    e.setStackTrace(newStackTrace);
    throw e;
}