博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
junit spring
阅读量:7117 次
发布时间:2019-06-28

本文共 1441 字,大约阅读时间需要 4 分钟。

import org.apache.commons.lang3.StringUtils;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.interceptor.annotations.After;
import com.opensymphony.xwork2.interceptor.annotations.Before;
public class UnitTestBean {
    private ClassPathXmlApplicationContext context;
    
    private String springXmlpath;
    
    public UnitTestBean(String springXmlpath){
        this.springXmlpath = springXmlpath;
    }
    
    @Before
    public void before(){
        System.out.println("springXmlpath = : "+springXmlpath);
        if(StringUtils.isEmpty(springXmlpath)){
            springXmlpath="classpath*:spring-*.xml";
        }
            context=new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
            System.out.println("context new :  "+springXmlpath.split("[,\\s]+"));
            context.start();
    }
    
    @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 context.getBean(clazz);
    }
    
    
}

 

 

 

 

 

import org.junit.runner.RunWith;

import org.junit.runners.BlockJUnit4ClassRunner;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestOneInterface extends UnitTestBean {
    public TestOneInterface() {
        super("classpath*:spring-ioc.xml");
    }
    
    @Test
    public void testSay() {
        OneInterface oneInterface = super.getBean("oneInterface");
        oneInterface.say("This is a test.");
    }
}

 

转载于:https://www.cnblogs.com/aiwoqu/p/4424822.html

你可能感兴趣的文章
iOS__上传应用到AppStore出现Authenticating with the iTunes store
查看>>
mac下设置eclipse自动提示
查看>>
IntelliJ IDEA中日志分类显示设置
查看>>
数据结构思维 第十六章 布尔搜索
查看>>
独家|从满天飞舞到逐步落地,自动驾驶好消息只会越来越多
查看>>
如何找到后台运行的隐藏程序
查看>>
多维防护:虚拟化安全挑战的破解之道
查看>>
Maven在Eclipse中的实用小技巧
查看>>
JdbcTemplate使用小结
查看>>
2014 网选 5011 Game(Nim游戏,数学题)
查看>>
微软官方windows phone开发视频教程第一天视频(附下载地址)
查看>>
螺旋阵列
查看>>
Gut基础入门(十)Git远程分支
查看>>
VC编写的程序不能在其他机器上运行的解决方案(续)
查看>>
不变模式-类行为型
查看>>
正则表达式学习笔记
查看>>
LVM2逻辑卷之1——创建及扩容
查看>>
grub.conf加密
查看>>
WSFC时间分区场景实作
查看>>
The Receiver 4.4 - 客户端硬件解码 - 大幅度提升3D显示效能
查看>>