org.springframework.test.context.TestContext Maven / Gradle / Ivy
Show all versions of spring-test Show documentation
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context;
import java.io.Serializable;
import java.lang.reflect.Method;
import org.springframework.context.ApplicationContext;
import org.springframework.core.AttributeAccessor;
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
/**
* {@code TestContext} encapsulates the context in which a test is executed,
* agnostic of the actual testing framework in use.
*
* @author Sam Brannen
* @since 2.5
*/
public interface TestContext extends AttributeAccessor, Serializable {
/**
* Get the {@link ApplicationContext application context} for this test
* context, possibly cached.
*
* Implementations of this method are responsible for loading the
* application context if the corresponding context has not already been
* loaded, potentially caching the context as well.
* @return the application context
* @throws IllegalStateException if an error occurs while retrieving the
* application context
*/
ApplicationContext getApplicationContext();
/**
* Get the {@link Class test class} for this test context.
* @return the test class (never {@code null})
*/
Class> getTestClass();
/**
* Get the current {@link Object test instance} for this test context.
*
Note: this is a mutable property.
* @return the current test instance (may be {@code null})
* @see #updateState(Object, Method, Throwable)
*/
Object getTestInstance();
/**
* Get the current {@link Method test method} for this test context.
*
Note: this is a mutable property.
* @return the current test method (may be {@code null})
* @see #updateState(Object, Method, Throwable)
*/
Method getTestMethod();
/**
* Get the {@link Throwable exception} that was thrown during execution of
* the {@link #getTestMethod() test method}.
*
Note: this is a mutable property.
* @return the exception that was thrown, or {@code null} if no
* exception was thrown
* @see #updateState(Object, Method, Throwable)
*/
Throwable getTestException();
/**
* Call this method to signal that the {@linkplain ApplicationContext application
* context} associated with this test context is dirty and should be
* discarded. Do this if a test has modified the context — for example,
* by replacing a bean definition or modifying the state of a singleton bean.
* @param hierarchyMode the context cache clearing mode to be applied if the
* context is part of a hierarchy (may be {@code null})
*/
void markApplicationContextDirty(HierarchyMode hierarchyMode);
/**
* Update this test context to reflect the state of the currently executing
* test.
*
Caution: concurrent invocations of this method might not be thread-safe,
* depending on the underlying implementation.
* @param testInstance the current test instance (may be {@code null})
* @param testMethod the current test method (may be {@code null})
* @param testException the exception that was thrown in the test method, or
* {@code null} if no exception was thrown
*/
void updateState(Object testInstance, Method testMethod, Throwable testException);
}