Spring整合Junit

1. 测试类中的问题和解决思路:

1.1 问题:

在测试类中,每个测试方法都有以下两行代码:

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);

这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常。所以又不能轻易删掉。

1.2 解决思路分析:

  • 1、应用程序的入口
    main方法
  • 2、junit单元测试中,没有main方法也能执行
    junit集成了一个main方法
    该方法就会判断当前测试类中哪些方法有 @Test注解
    junit就让有Test注解的方法执行
  • 3、junit不会管我们是否采用spring框架
    在执行测试方法时,junit根本不知道我们是不是使用了spring框架
    所以也就不会为我们读取配置文件/配置类创建spring核心容器
  • 4、由以上三点可知
    当测试方法执行时,没有Ioc容器,就算写了Autowired注解,也无法实现注入
  • 我们都知道,junit 单元测试的原理(在 web 阶段课程中讲过),但显然,junit 是无法实现的,因为它自己都无法知晓我们是否使用了 spring 框架,更不用说帮我们创建 spring 容器了。不过好在,junit 给我们暴露了一个注解,可以让我们替换掉它的运行器。
    这时,我们需要依靠 spring 框架,因为它提供了一个运行器,可以读取配置文件(或注解)来创建容器。我们只需要告诉它配置文件在哪就行了。

2.配置步骤:

使用Junit单元测试,测试我们的配置

  • 当测试方法执行时,没有IOC容器,就算写了AutoWired注解,也无法注入
  • Spring真和Junit的配置:
  • 1.导入Spring整合junit的jar包(坐标)
  • 2.使用junit提供的一个注解把原有的main方法替换了,替换成spring提供的
  •      @Runwith
    
  • 3.告知spring的运行器,spring的ioc创建是基于xml还是注解的,并且说明位置
  •   @ContextConfiguration
    
  •       location:指定xml文件的位置,加上classpath关键字,表示在类路径下
    
  •       classes:指定注解所在的位置
    
  • .使用@Autowired给测试类中的变量注入数据
  • 当我们使用spring 5.x版本时,要求junit的jar包必须是4.12及以上

3.整合之后的测试类如下:

package com.itheima.test;

import com.itheima.domain.Account;
import com.itheima.service.IAccountService;
import config.SpringConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/*
* 使用Junit单元测试,测试我们的配置
* 当测试方法执行时,没有IOC容器,就算写了AutoWired注解,也无法注入
* Spring真和Junit的配置:
*   1.导入Spring整合junit的jar包(坐标)
*   2.使用junit提供的一个注解把原有的main方法替换了,替换成spring提供的
*          @Runwith
*   3.告知spring的运行器,spring的ioc创建是基于xml还是注解的,并且说明位置
*       @ContextConfiguration
*           location:指定xml文件的位置,加上classpath关键字,表示在类路径下
*           classes:指定注解所在的位置
* 4.使用@Autowired给测试类中的变量注入数据
* 当我们使用spring 5.x版本时,要求junit的jar包必须是4.12及以上
* */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {

    @Autowired
    private IAccountService as;


    @Test
    public void testFindAll(){
        //3.执行方法
        List<Account> accounts=as.findAllAccount();
        for(Account account:accounts){
            System.out.println(account);
        }

    }
    @Test
    public void testFindOne(){
        //3.执行方法
        Account account=as.findAccountById(1);
        System.out.println(account);
    }
    @Test
    public void testSave(){
        Account account=new Account();
        account.setName("test anno");
        account.setMoney(12345F);
        //3.执行方法
        as.saveAccount(account);
    }
    @Test
    public void testUpdate(){
        //3.执行方法
        Account account=as.findAccountById(5);
        account.setMoney(23456F);
        as.updateAccount(account);
    }
    @Test
    public void testDelete(){
        //3.执行方法
        as.deleteAccount(5);
    }

}

4.为什么不把测试类配到xml中呢?

在解释这个问题之前,先解除大家的疑虑,配到 XML 中能不能用呢?
答案是肯定的,没问题,可以使用。
那么为什么不采用配置到 xml 中的方式呢?
这个原因是这样的:
第一:当我们在 xml 中配置了一个 bean,spring 加载配置文件创建容器时,就会创建对象。
第二:测试类只是我们在测试功能时使用,而在项目中它并不参与程序逻辑,也不会解决需求上的问
题,所以创建完了,并没有使用。那么存在容器中就会造成资源的浪费。
所以,基于以上两点,我们不应该把测试配置到 xml 文件中。

5.主配置文件如下:

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>day02_eesy_04account_annoioc_withoutxml</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>
posted @   别团等shy哥发育  阅读(15)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示