/** * Get the {@code exception} that the supplied {@linkplain FrameworkMethod * test method} is expected to throw. * <p>Supports JUnit's {@link Test#expected() @Test(expected=...)} annotation. * <p>Can be overridden by subclasses. * @return the expected exception, or {@code null} if none was specified */ @Nullable protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) { Test test = frameworkMethod.getAnnotation(Test.class); return (test != null && test.expected() != Test.None.class ? test.expected() : null); }
@Override public void evaluate() throws Throwable { LOG.info("RUNNING TEST METHOD {}", name); try { super.evaluate(); Runtime rt = Runtime.getRuntime(); long usedKB = (rt.totalMemory() - rt.freeMemory()) / 1024; LOG.info("Memory used {}", usedKB); ThreadGroup tg = Thread.currentThread().getThreadGroup(); while (tg.getParent() != null) { tg = tg.getParent(); } LOG.info("Number of threads {}", tg.activeCount()); } catch (Throwable t) { // The test method threw an exception, but it might be an // expected exception as defined in the @Test annotation. // Check the annotation and log an appropriate message. Test annotation = this.method.getAnnotation(Test.class); if (annotation != null && annotation.expected() != null && annotation.expected().isAssignableFrom(t.getClass())) { LOG.info("TEST METHOD {} THREW EXPECTED EXCEPTION {}", name, annotation.expected()); } else { LOG.info("TEST METHOD FAILED {}", name, t); } throw t; } LOG.info("FINISHED TEST METHOD {}", name); } }
@Override public Statement apply(Statement statement, Description description) { RetryOnFailure retryOnFailure = description.getAnnotation(RetryOnFailure.class); RetryOnException retryOnException = description.getAnnotation(RetryOnException.class); // sanity check that we don't use expected exceptions with the RetryOnX annotations if (retryOnFailure != null || retryOnException != null) { Test test = description.getAnnotation(Test.class); if (test.expected() != Test.None.class) { throw new IllegalArgumentException("You cannot combine the RetryOnFailure " + "annotation with the Test(expected) annotation."); } } // sanity check that we don't use both annotations if (retryOnFailure != null && retryOnException != null) { throw new IllegalArgumentException( "You cannot combine the RetryOnFailure and RetryOnException annotations."); } if (retryOnFailure != null) { return new RetryOnFailureStatement(retryOnFailure.times(), statement); } else if (retryOnException != null) { return new RetryOnExceptionStatement(retryOnException.times(), retryOnException.exception(), statement); } else { return statement; } }
/** * Get the {@code exception} that the supplied {@linkplain FrameworkMethod * test method} is expected to throw. * <p>Supports JUnit's {@link Test#expected() @Test(expected=...)} annotation. * <p>Can be overridden by subclasses. * @return the expected exception, or {@code null} if none was specified */ @Nullable protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) { Test test = frameworkMethod.getAnnotation(Test.class); return (test != null && test.expected() != Test.None.class ? test.expected() : null); }
public Statement createStatement(final TestClass testClass, final FrameworkMethod method, final Object target, final Statement next, final Description description, final RunNotifier notifier) { final Test annotation = method.getAnnotation(Test.class); return annotation.expected() == Test.None.class ? next : new ExpectException(next, annotation.expected()); } }
@MayReturnNull private Class<? extends Throwable> getExpectedException() { Test test = frameworkMethod.getAnnotation(Test.class); if (test == null) { return None.class; } return test.expected(); }
/** * test for {@code Annotation} values. */ @Test public void annotation_convert() { Test annotation = description.getAnnotation(Test.class); AnnotationInfo info = (AnnotationInfo) ValueInfo.of(annotation); assertThat(info.getObject(), info.getDeclaringClass(), is(ClassInfo.of(Test.class))); assertThat(info.getElements(), hasEntry("expected", ClassInfo.of(annotation.expected()))); assertThat(info.getElements(), hasEntry("timeout", LongInfo.of(annotation.timeout()))); }