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