001/*
002 * Copyright 2002-2019 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;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.junit.runner.RunWith;
022
023import org.springframework.context.ApplicationContext;
024import org.springframework.context.ApplicationContextAware;
025import org.springframework.lang.Nullable;
026import org.springframework.test.context.ContextConfiguration;
027import org.springframework.test.context.TestContext;
028import org.springframework.test.context.TestContextManager;
029import org.springframework.test.context.TestExecutionListeners;
030import org.springframework.test.context.event.EventPublishingTestExecutionListener;
031import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
032import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
033import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
034import org.springframework.test.context.web.ServletTestExecutionListener;
035
036/**
037 * Abstract base test class which integrates the <em>Spring TestContext
038 * Framework</em> with explicit {@link ApplicationContext} testing support
039 * in a <strong>JUnit 4</strong> environment.
040 *
041 * <p>Concrete subclasses should typically declare a class-level
042 * {@link ContextConfiguration @ContextConfiguration} annotation to
043 * configure the {@linkplain ApplicationContext application context} {@linkplain
044 * ContextConfiguration#locations() resource locations} or {@linkplain
045 * ContextConfiguration#classes() component classes}. <em>If your test does not
046 * need to load an application context, you may choose to omit the
047 * {@link ContextConfiguration @ContextConfiguration} declaration and to configure
048 * the appropriate {@link org.springframework.test.context.TestExecutionListener
049 * TestExecutionListeners} manually.</em>
050 *
051 * <p>The following {@link org.springframework.test.context.TestExecutionListener
052 * TestExecutionListeners} are configured by default:
053 *
054 * <ul>
055 * <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
056 * <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
057 * <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
058 * <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
059 * <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener}
060 * </ul>
061 *
062 * <p>This class serves only as a convenience for extension.
063 * <ul>
064 * <li>If you do not wish for your test classes to be tied to a Spring-specific
065 * class hierarchy, you may configure your own custom test classes by using
066 * {@link SpringRunner}, {@link ContextConfiguration @ContextConfiguration},
067 * {@link TestExecutionListeners @TestExecutionListeners}, etc.</li>
068 * <li>If you wish to extend this class and use a runner other than the
069 * {@link SpringRunner}, you can use
070 * {@link org.springframework.test.context.junit4.rules.SpringClassRule SpringClassRule} and
071 * {@link org.springframework.test.context.junit4.rules.SpringMethodRule SpringMethodRule}
072 * and specify your runner of choice via {@link RunWith @RunWith(...)}.</li>
073 * </ul>
074 *
075 * <p><strong>NOTE:</strong> This class requires JUnit 4.12 or higher.
076 *
077 * @author Sam Brannen
078 * @since 2.5
079 * @see ContextConfiguration
080 * @see TestContext
081 * @see TestContextManager
082 * @see TestExecutionListeners
083 * @see ServletTestExecutionListener
084 * @see DirtiesContextBeforeModesTestExecutionListener
085 * @see DependencyInjectionTestExecutionListener
086 * @see DirtiesContextTestExecutionListener
087 * @see EventPublishingTestExecutionListener
088 * @see AbstractTransactionalJUnit4SpringContextTests
089 * @see org.springframework.test.context.testng.AbstractTestNGSpringContextTests
090 */
091@RunWith(SpringRunner.class)
092@TestExecutionListeners({ ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
093        DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
094        EventPublishingTestExecutionListener.class })
095public abstract class AbstractJUnit4SpringContextTests implements ApplicationContextAware {
096
097        /**
098         * Logger available to subclasses.
099         */
100        protected final Log logger = LogFactory.getLog(getClass());
101
102        /**
103         * The {@link ApplicationContext} that was injected into this test instance
104         * via {@link #setApplicationContext(ApplicationContext)}.
105         */
106        @Nullable
107        protected ApplicationContext applicationContext;
108
109
110        /**
111         * Set the {@link ApplicationContext} to be used by this test instance,
112         * provided via {@link ApplicationContextAware} semantics.
113         * @param applicationContext the ApplicationContext that this test runs in
114         */
115        @Override
116        public final void setApplicationContext(final ApplicationContext applicationContext) {
117                this.applicationContext = applicationContext;
118        }
119
120}