使用Spring-Test对Spring框架进行单元测试

来源:互联网 发布:南京行知幼儿园怎么样 编辑:程序博客网 时间:2024/05/24 11:14

使用Spring-Test对Spring框架进行单元测试

配置过程:

加载依赖:

引入Maven依赖:

        <!--spring单元测试依赖 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${springframework}</version>            <scope>test</scope>        </dependency>

编写SpringTestBase基础类,加载所需xml文件:

import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@ContextConfiguration(locations = { "classpath:Application-Redis.xml" })@RunWith(SpringJUnit4ClassRunner.class)public class SpringTestBase extends AbstractJUnit4SpringContextTests {}

将所需加载的xml文件指定为locations的value。

编写单元测试类 示例:

直接继承SpringTestBase 就可以对Spring框架的内容进行单元测试。

import org.junit.Assert;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;public class TestRedisCacheDaoImpl extends SpringTestBase {    @Autowired    public RedisCacheDaoImpl redisCacheDaoImpl;    @Test    public void testPing() {        boolean reslut = redisCacheDaoImpl.ping();        Assert.assertEquals(true, reslut);    }}
原创粉丝点击