Spring入门篇之单元测试

这里写图片描述

1.

package com.wuyonghu.junit;

import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

public class UnitTestBase {
    // 这个context就是上下文容器
    private ClassPathXmlApplicationContext context;
    private String springXmlpath;

    public UnitTestBase() {
    };

    public UnitTestBase(String springXmlpath) {
    //传入的参数就是spring配置bean的配置文件的路径
        this.springXmlpath = springXmlpath;
    }

    @Before
    public void before() {
    //如果为空时候的处理方式
        if (StringUtils.isEmpty(springXmlpath)) {
            springXmlpath = "classpath*:spring-*.xml";
        }
        try {
            context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
            // 装载bean:将配置文件中的bean加载到context上下文中
            context.start();
        } catch (BeansException e) {
            e.printStackTrace();
        }
    }

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

    @SuppressWarnings("unchecked")
    protected <T extends Object> T getBean(String beanId) {
        // 获取到上下文中的bean的方法
        return (T) context.getBean(beanId);
    }

    protected <T extends Object> T getBean(Class<T> clazz) {
        // 获取到上下文中的bean的方法
        return (T) context.getBean(clazz);
    }

}

2.配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean id="oneInterface" class="com.wuyonghu.faceinterface.OneInterfaceImple"></bean>

</beans>

3.测试代码:

package com.wuyonghu.faceinterface;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.wuyonghu.junit.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class ConfigTest  extends  UnitTestBase{
    public ConfigTest(){
        super("classpath*:applicationContext.xml");
    }
    @Test
    public void testHello(){
        OneInterface oneInterface=super.getBean("oneInterface");
        System.out.println(oneInterface.hello("单元测试"));
    }
}

以上使用到的是通過继承的方法,如果难以理解则可以直接这样:

@Test
    public void testHello2() {
    //获取到bean容器,其中applicationContext.xml就是spring配置文件
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //通过getBean方法从上下文中获取到bean对象
        OneInterface bean = (OneInterface) context.getBean("oneInterface");
        System.out.println(bean.hello("classpath方式获取"));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值