Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using the latest version of JUnit you can extend the default test runner to handle this for you (without having to wrap each of your methods in a try/catch block)</p> <p>ExtendedTestRunner.java - New test runner:</p> <pre><code>public class ExtendedTestRunner extends BlockJUnit4ClassRunner { public ExtendedTestRunner( Class&lt;?&gt; clazz ) throws InitializationError { super( clazz ); } @Override protected Statement possiblyExpectingExceptions( FrameworkMethod method, Object test, Statement next ) { ExtendedTest annotation = method.getAnnotation( ExtendedTest.class ); return expectsCauseException( annotation ) ? new ExpectCauseException( next, getExpectedCauseException( annotation ) ) : super.possiblyExpectingExceptions( method, test, next ); } @Override protected List&lt;FrameworkMethod&gt; computeTestMethods() { Set&lt;FrameworkMethod&gt; testMethods = new HashSet&lt;FrameworkMethod&gt;( super.computeTestMethods() ); testMethods.addAll( getTestClass().getAnnotatedMethods( ExtendedTest.class ) ); return testMethods; } @Override protected void validateTestMethods( List&lt;Throwable&gt; errors ) { super.validateTestMethods( errors ); validatePublicVoidNoArgMethods( ExtendedTest.class, false, errors ); } private Class&lt;? extends Throwable&gt; getExpectedCauseException( ExtendedTest annotation ) { if (annotation == null || annotation.expectedCause() == ExtendedTest.None.class) return null; else return annotation.expectedCause(); } private boolean expectsCauseException( ExtendedTest annotation) { return getExpectedCauseException(annotation) != null; } } </code></pre> <p>ExtendedTest.java - annotation to mark test methods with:</p> <pre><code>@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface ExtendedTest { /** * Default empty exception */ static class None extends Throwable { private static final long serialVersionUID= 1L; private None() { } } Class&lt;? extends Throwable&gt; expectedCause() default None.class; } </code></pre> <p>ExpectCauseException.java - new JUnit Statement:</p> <pre><code>public class ExpectCauseException extends Statement { private Statement fNext; private final Class&lt;? extends Throwable&gt; fExpected; public ExpectCauseException( Statement next, Class&lt;? extends Throwable&gt; expected ) { fNext= next; fExpected= expected; } @Override public void evaluate() throws Exception { boolean complete = false; try { fNext.evaluate(); complete = true; } catch (Throwable e) { if ( e.getCause() == null || !fExpected.isAssignableFrom( e.getCause().getClass() ) ) { String message = "Unexpected exception cause, expected&lt;" + fExpected.getName() + "&gt; but was&lt;" + ( e.getCause() == null ? "none" : e.getCause().getClass().getName() ) + "&gt;"; throw new Exception(message, e); } } if (complete) throw new AssertionError( "Expected exception cause: " + fExpected.getName()); } } </code></pre> <p>Usage:</p> <pre><code>@RunWith( ExtendedTestRunner.class ) public class MyTests { @ExtendedTest( expectedCause = MyException.class ) public void someMethod() { throw new RuntimeException( new MyException() ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload