java - How to create custom JUnit4 assertions that don't show in the Failure Trace -
i'd add custom assertions our code base hide failure trace. know how write public static method can statically import. know how reuse old assertions or throw new assertionerror
.
what can't figure out how keep new custom assertions out of failure trace. we're used first hit in failure trace not being assertion code test code called assertion.
i know there filtertrace
attribute controls filtering stack can't find documentation of i'd have add new assertions filter.
an example of want do:
package testassertions; import static newassertions.myassertions.myasserttrue; import org.junit.test; public class exampletest { @test public void myasserttruepassing() { myasserttrue(true); } @test public void myasserttruefailing() { myasserttrue(false); } }
package newassertions; import static org.junit.assert.asserttrue; public class myassertions { public static void myasserttrue(boolean b) { asserttrue(b); } }
failure trace of myasserttruefailing() shows:
java.lang.assertionerror @ newassertions.myassertions.myasserttrue(myassertions.java:8) @ testassertions.exampletest.myasserttruefailing(exampletest.java:12)
i need show:
java.lang.assertionerror @ testassertions.exampletest.myasserttruefailing(exampletest.java:12)
as mentioned in another question cleaning noise stack traces, filtering classes within ide easiest solution. in fact, stack traces you've shown in question filtered.
if wanted in code, add filtering custom assertion class below:
package newassertions; import static org.junit.assert.asserttrue; import java.util.arraylist; public class myassertions { public static void myasserttrue(boolean b) { try { asserttrue(b); } catch (assertionerror e) { filterstacktrace(e); throw e; } } private static void filterstacktrace(assertionerror error) { stacktraceelement[] stacktrace = error.getstacktrace(); if (null != stacktrace) { arraylist<stacktraceelement> filteredstacktrace = new arraylist<stacktraceelement>(); (stacktraceelement e : stacktrace) { if (!"newassertions.myassertions".equals(e.getclassname())) { filteredstacktrace.add(e); } } error.setstacktrace(filteredstacktrace.toarray(new stacktraceelement[0])); } } }
the name of enclosing class 'newassertions.myassertions' (hard-coded) filtered stack trace in example. mechanism work filter stack trace assertionerror create , not raised other assertions.
Comments
Post a Comment