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

时间:2022-09-15 07:43:39

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

配置过程:

加载依赖

引入Maven依赖:

?
1
2
3
4
5
6
7
<!--spring单元测试依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${springframework}</version>
    <scope>test</scope>
</dependency>

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

?
1
2
3
4
5
6
7
8
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框架的内容进行单元测试。

?
1
2
3
4
5
6
7
8
9
10
11
12
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当中添加依赖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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

?
1
2
3
4
5
6
@Service
public class HelloService {
    public String hello(String name) {
        return "hello " + name;
    }
}

添加配置文件扫描包:

?
1
2
3
4
5
6
7
8
9
10
<?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、测试方法

?
1
2
3
4
5
6
7
8
9
10
@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"));
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u011389474/article/details/72303118