继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

junit基本测试类 BaseTest

梵天001
关注TA
已关注
手记 4
粉丝 5
获赞 38

package introduction;
import org.apache.commons.lang3.StringUtils;
import org.apache.*;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BaseTest {

private ClassPathXmlApplicationContext context;  

private String springXmlpath;  

public BaseTest() {}  

public BaseTest(String springXmlpath) {  
    this.springXmlpath = springXmlpath;  
}  

@Before  
public void before() {  
    if (StringUtils.isEmpty(springXmlpath)) {  
        springXmlpath = "classpath*:spring-*.xml";  
    }  
    try {  
        context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));  
        context.start();  
    } catch (BeansException e) {  
        e.printStackTrace();  
    }  
}  

@After  
public void after() {  
    context.destroy();  
}  

@SuppressWarnings("unchecked")  
protected <T extends Object> T getBean(String beanId) {  
    try {  
        return (T)context.getBean(beanId);  
    } catch (BeansException e) {  
        e.printStackTrace();  
        return null;  
    }  
}  

protected <T extends Object> T getBean(Class<T> clazz) {  
    try {  
        return context.getBean(clazz);  
    } catch (BeansException e) {  
        e.printStackTrace();  
        return null;  
    }  
}  

}

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP