001/* 002 * Copyright 2002-2018 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 RunBeforeTestExecutionCallbacks} is a custom JUnit {@link Statement} 027 * which allows the <em>Spring TestContext Framework</em> to be plugged into the 028 * JUnit 4 execution chain by calling {@link TestContextManager#beforeTestExecution 029 * beforeTestExecution()} on the supplied {@link TestContextManager}. 030 * 031 * @author Sam Brannen 032 * @since 5.0 033 * @see #evaluate() 034 * @see RunAfterTestExecutionCallbacks 035 */ 036public class RunBeforeTestExecutionCallbacks 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 RunBeforeTestExecutionCallbacks} 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 beforeTestExecution()} 055 */ 056 public RunBeforeTestExecutionCallbacks(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 * Invoke {@link TestContextManager#beforeTestExecution(Object, Method)} 067 * and then evaluate the next {@link Statement} in the execution chain 068 * (typically an instance of 069 * {@link org.junit.internal.runners.statements.InvokeMethod InvokeMethod}). 070 */ 071 @Override 072 public void evaluate() throws Throwable { 073 this.testContextManager.beforeTestExecution(this.testInstance, this.testMethod); 074 this.next.evaluate(); 075 } 076 077}