【一个特别的工具类——UnitTestBase】
原创
©著作权归作者所有:来自51CTO博客作者梦幻烟花一刹那的原创作品,请联系作者获取转载授权,否则将追究法律责任
之前没有上传的一个工具类
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTestBase {
private ClassPathXmlApplicationContext context;
private String stringXmlPath;
public UnitTestBase() {
};
public UnitTestBase(String stringXmlPath) {
this.stringXmlPath = stringXmlPath;
};
@Before
public void before() {
if (stringXmlPath == null || stringXmlPath.equals("")) {
stringXmlPath = "classPath*:spring-*.xml";
}
try {
context = new ClassPathXmlApplicationContext(stringXmlPath.split("[,\\s]+"));
context.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void after() {
context.destroy();
}
@SuppressWarnings("unchecked")
protected <T extends Object> T getBean(String beanId) {
return (T) context.getBean(beanId);
}
protected <T extends Object> T getBean(Class<T> clazz) {
return (T) context.getBean(clazz);
}
}