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.lang.reflect.Method; 020 021import org.junit.runners.model.Statement; 022 023import org.springframework.test.context.TestContextManager; 024 025/** 026 * {@code RunBeforeTestMethodCallbacks} is a custom JUnit {@link Statement} which allows 027 * the <em>Spring TestContext Framework</em> to be plugged into the JUnit execution chain 028 * by calling {@link TestContextManager#beforeTestMethod(Object, Method) 029 * beforeTestMethod()} on the supplied {@link TestContextManager}. 030 * 031 * @author Sam Brannen 032 * @since 3.0 033 * @see #evaluate() 034 * @see RunAfterTestMethodCallbacks 035 */ 036public class RunBeforeTestMethodCallbacks extends Statement { 037 038 private final Statement next; 039 040 private final Object testInstance; 041 042 private final Method testMethod; 043 044 private final TestContextManager testContextManager; 045 046 047 /** 048 * Construct a new {@code RunBeforeTestMethodCallbacks} statement. 049 * @param next the next {@code Statement} in the execution chain 050 * @param testInstance the current test instance (never {@code null}) 051 * @param testMethod the test method which is about to be executed on the 052 * test instance 053 * @param testContextManager the TestContextManager upon which to call 054 * {@code beforeTestMethod()} 055 */ 056 public RunBeforeTestMethodCallbacks(Statement next, Object testInstance, Method testMethod, 057 TestContextManager testContextManager) { 058 059 this.next = next; 060 this.testInstance = testInstance; 061 this.testMethod = testMethod; 062 this.testContextManager = testContextManager; 063 } 064 065 066 /** 067 * Invoke {@link TestContextManager#beforeTestMethod(Object, Method)} 068 * and then evaluate the next {@link Statement} in the execution chain 069 * (typically an instance of 070 * {@link org.junit.internal.runners.statements.RunBefores RunBefores}). 071 */ 072 @Override 073 public void evaluate() throws Throwable { 074 this.testContextManager.beforeTestMethod(this.testInstance, this.testMethod); 075 this.next.evaluate(); 076 } 077 078}