001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.context.junit4.statements; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.junit.runners.model.MultipleFailureException; 023import org.junit.runners.model.Statement; 024 025import org.springframework.test.context.TestContextManager; 026 027/** 028 * {@code RunAfterTestClassCallbacks} is a custom JUnit {@link Statement} which allows 029 * the <em>Spring TestContext Framework</em> to be plugged into the JUnit execution chain 030 * by calling {@link TestContextManager#afterTestClass afterTestClass()} on the supplied 031 * {@link TestContextManager}. 032 * 033 * <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher. 034 * 035 * @author Sam Brannen 036 * @since 3.0 037 * @see #evaluate() 038 * @see RunBeforeTestClassCallbacks 039 */ 040public class RunAfterTestClassCallbacks extends Statement { 041 042 private final Statement next; 043 044 private final TestContextManager testContextManager; 045 046 047 /** 048 * Construct a new {@code RunAfterTestClassCallbacks} statement. 049 * @param next the next {@code Statement} in the execution chain 050 * @param testContextManager the TestContextManager upon which to call 051 * {@code afterTestClass()} 052 */ 053 public RunAfterTestClassCallbacks(Statement next, TestContextManager testContextManager) { 054 this.next = next; 055 this.testContextManager = testContextManager; 056 } 057 058 059 /** 060 * Evaluate the next {@link Statement} in the execution chain (typically an instance of 061 * {@link org.junit.internal.runners.statements.RunAfters RunAfters}), catching any 062 * exceptions thrown, and then invoke {@link TestContextManager#afterTestClass()}. 063 * <p>If the invocation of {@code afterTestClass()} throws an exception, it will also 064 * be tracked. Multiple exceptions will be combined into a {@link MultipleFailureException}. 065 */ 066 @Override 067 public void evaluate() throws Throwable { 068 List<Throwable> errors = new ArrayList<Throwable>(); 069 try { 070 this.next.evaluate(); 071 } 072 catch (Throwable ex) { 073 errors.add(ex); 074 } 075 076 try { 077 this.testContextManager.afterTestClass(); 078 } 079 catch (Throwable ex) { 080 errors.add(ex); 081 } 082 083 MultipleFailureException.assertEmpty(errors); 084 } 085 086}