如何使用Spring-Test對Spring框架進行單元測試

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);
    }
}

Spring-Test測試數據

1、新建一個maven項目

2、pom.xml當中添加依賴

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>   
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

spring-test是Spring當中的測試包,而junit是單元測試包,結合起來使用。

添加一個bean,便於測試。

HelloService.java

@Service
public class HelloService {
    public String hello(String name) {
        return "hello " + name;
    } 
}

添加配置文件掃描包:

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--將除瞭 Controller 之外的所有 Bean 註冊到 Spring 容器中-->
    <context:component-scan base-package="org.youyuan" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.youyuan.controller"/>
    </context:component-scan> 
</beans>

3、測試方法

@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class Test { 
    @Autowired
    HelloService helloService;
    @org.junit.Test
    public void test(){
        System.out.println(helloService.hello("youyuan"));
    }
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: