List of usage examples for java.lang AssertionError getStackTrace
public StackTraceElement[] getStackTrace()
From source file:com.mta.sharedutils.Util.java
/** * Some low level stuff to make the exception point to the correct method throwing the exception. * Or else all the assertions will point to the "assertTrue" method as the crash point. * * @param comment//from w w w .ja v a2 s . c o m * @return */ private static AssertionError generateAssertException(String comment) { AssertionError exception = new AssertionError(comment); // you can add some more info to the comment reported to you, for example .. = new AssertionError(comment + "/" + DEBUG_PROCESS_ID) : StackTraceElement[] stackTrace = exception.getStackTrace(); StackTraceElement[] newArray = Arrays.copyOfRange(stackTrace, 2, stackTrace.length); exception.setStackTrace(newArray); return exception; }
From source file:com.cedarsoft.serialization.test.utils.AbstractXmlSerializerMultiTest.java
@Override protected void verifySerialized(@Nonnull List<? extends byte[]> serialized) throws Exception { List<? extends String> expected = getExpectedSerialized(); int index = 0; for (byte[] current : serialized) { String expectedWithNamespace = AbstractXmlSerializerTest2.addNameSpace( (AbstractXmlSerializer<?, ?, ?, ?>) getSerializer(), expected.get(index).getBytes()); try {//from w w w .j ava 2s . co m AssertUtils.assertXMLEquals(new String(current, getCharset()), expectedWithNamespace); } catch (AssertionError e) { AssertionError newError = new AssertionError("Failed for index <" + index + ">: " + e.getMessage()); newError.setStackTrace(e.getStackTrace()); throw newError; } index++; } }
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>/*from w w w. jav a2 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; }