TestContext.java 4.4 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
J
Juergen Hoeller 已提交
2
 * Copyright 2002-2019 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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;

19
import java.io.Serializable;
A
Arjen Poutsma 已提交
20 21 22
import java.lang.reflect.Method;

import org.springframework.context.ApplicationContext;
23
import org.springframework.core.AttributeAccessor;
24
import org.springframework.lang.Nullable;
25
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
A
Arjen Poutsma 已提交
26 27

/**
28
 * {@code TestContext} encapsulates the context in which a test is executed,
29
 * agnostic of the actual testing framework in use.
30
 *
31 32 33 34 35 36 37 38
 * <p>As of Spring Framework 5.0, concrete implementations are highly encouraged
 * to implement a <em>copy constructor</em> in order to allow the immutable state
 * and attributes of a {@code TestContext} to be used as a template for additional
 * contexts created for parallel test execution. The copy constructor must accept a
 * single argument of the type of the concrete implementation. Any implementation
 * that does not provide a copy constructor will likely fail in an environment
 * that executes tests concurrently.
 *
A
Arjen Poutsma 已提交
39 40
 * @author Sam Brannen
 * @since 2.5
J
Juergen Hoeller 已提交
41 42
 * @see TestContextManager
 * @see TestExecutionListener
A
Arjen Poutsma 已提交
43
 */
44
public interface TestContext extends AttributeAccessor, Serializable {
A
Arjen Poutsma 已提交
45 46

	/**
S
Sam Brannen 已提交
47 48
	 * Get the {@linkplain ApplicationContext application context} for this
	 * test context, possibly cached.
49 50 51
	 * <p>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.
J
Juergen Hoeller 已提交
52
	 * @return the application context (never {@code null})
53 54
	 * @throws IllegalStateException if an error occurs while retrieving the
	 * application context
A
Arjen Poutsma 已提交
55
	 */
56
	ApplicationContext getApplicationContext();
A
Arjen Poutsma 已提交
57 58

	/**
S
Sam Brannen 已提交
59
	 * Get the {@linkplain Class test class} for this test context.
60
	 * @return the test class (never {@code null})
A
Arjen Poutsma 已提交
61
	 */
62
	Class<?> getTestClass();
A
Arjen Poutsma 已提交
63 64

	/**
S
Sam Brannen 已提交
65
	 * Get the current {@linkplain Object test instance} for this test context.
S
Sam Brannen 已提交
66
	 * <p>Note: this is a mutable property.
J
Juergen Hoeller 已提交
67
	 * @return the current test instance (never {@code null})
68
	 * @see #updateState(Object, Method, Throwable)
A
Arjen Poutsma 已提交
69
	 */
70
	Object getTestInstance();
A
Arjen Poutsma 已提交
71 72

	/**
S
Sam Brannen 已提交
73
	 * Get the current {@linkplain Method test method} for this test context.
S
Sam Brannen 已提交
74
	 * <p>Note: this is a mutable property.
J
Juergen Hoeller 已提交
75
	 * @return the current test method (never {@code null})
A
Arjen Poutsma 已提交
76 77
	 * @see #updateState(Object, Method, Throwable)
	 */
78
	Method getTestMethod();
A
Arjen Poutsma 已提交
79 80

	/**
S
Sam Brannen 已提交
81 82
	 * Get the {@linkplain Throwable exception} that was thrown during execution
	 * of the {@linkplain #getTestMethod() test method}.
S
Sam Brannen 已提交
83
	 * <p>Note: this is a mutable property.
84
	 * @return the exception that was thrown, or {@code null} if no exception was thrown
A
Arjen Poutsma 已提交
85 86
	 * @see #updateState(Object, Method, Throwable)
	 */
87
	@Nullable
88
	Throwable getTestException();
89 90 91 92

	/**
	 * Call this method to signal that the {@linkplain ApplicationContext application
	 * context} associated with this test context is <em>dirty</em> and should be
S
Sam Brannen 已提交
93 94 95 96
	 * removed from the context cache.
	 * <p>Do this if a test has modified the context &mdash; for example, by
	 * modifying the state of a singleton bean, modifying the state of an embedded
	 * database, etc.
97 98 99
	 * @param hierarchyMode the context cache clearing mode to be applied if the
	 * context is part of a hierarchy (may be {@code null})
	 */
100
	void markApplicationContextDirty(@Nullable HierarchyMode hierarchyMode);
A
Arjen Poutsma 已提交
101 102

	/**
103
	 * Update this test context to reflect the state of the currently executing test.
104 105
	 * <p>Caution: concurrent invocations of this method might not be thread-safe,
	 * depending on the underlying implementation.
106 107
	 * @param testInstance the current test instance (may be {@code null})
	 * @param testMethod the current test method (may be {@code null})
108 109
	 * @param testException the exception that was thrown in the test method,
	 * or {@code null} if no exception was thrown
A
Arjen Poutsma 已提交
110
	 */
111
	void updateState(@Nullable Object testInstance, @Nullable Method testMethod, @Nullable Throwable testException);
A
Arjen Poutsma 已提交
112 113

}