1 package org.jencks; 2 3 import junit.framework.TestCase; 4 import org.springframework.context.ConfigurableApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 /*** 8 * @version $Revision: 1.2 $ 9 */ 10 public abstract class SpringTestSupport extends TestCase { 11 protected ConfigurableApplicationContext applicationContext; 12 13 protected void setUp() throws Exception { 14 applicationContext = createApplicationContext(); 15 assertNotNull("Should have an ApplicationContext", applicationContext); 16 } 17 18 19 protected void tearDown() throws Exception { 20 if (applicationContext != null) { 21 applicationContext.close(); 22 } 23 } 24 25 protected ConfigurableApplicationContext createApplicationContext() { 26 return new ClassPathXmlApplicationContext(getApplicationContextXml()); 27 } 28 29 protected abstract String getApplicationContextXml(); 30 31 /*** 32 * Finds the mandatory bean in the application context failing if its not there 33 */ 34 protected Object getBean(String name) { 35 Object answer = applicationContext.getBean(name); 36 assertNotNull("Could not find bean in ApplicationContext called: " + name, answer); 37 return answer; 38 } 39 }