Spring整合JUnit详解

 更新时间:2023年04月17日 11:13:46   作者:@每天都要敲代码  
Spring 是目前主流的 Java Web 开发框架,是 Java 世界最为成功的框架。该框架是一个轻量级的开源框架,这篇文章主要介绍如何配置数据源、注解开发以及整合Junit,感兴趣的同学可以参考一下

一:Spring整合JUnit

1. Spring对JUnit4的支持

准备工作:pom.xml

注:以前是直接使用单元测试Junit,现在使用Spring对Junit的整合!

<?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.bjpowernode</groupId>
    <artifactId>spring6-014-junit</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <!--仓库-->
    <repositories>
        <!--spring里程碑版本的仓库-->
        <repository>
            <id>repository.spring.milestone</id>
            <name>Spring Milestone Repository</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
 
    <dependencies>
        <!--spring context依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.0-M2</version>
        </dependency>
 
        <!--spring对junit的支持相关依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <!--这个版本spring6,既支持Junit4又支持Junit5依赖-->
            <version>6.0.0-M2</version>
        </dependency>
 
        <!--junit4依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
 
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
 
</project>

声明Bean

package com.bjpowernode.spring6.bean;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("user") // 纳入spring管理
public class User {
    @Value("张三") // 通过注解的方式进行赋值
    private String name;
 
    public User(String name) {
        this.name = name;
    }
    public User() {
    }
 
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}

spring.xml配置

<?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 http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描组件-->
    <context:component-scan base-package="com.bjpowernode.spring6.bean"/>
    
</beans>

单元测试:

①以前的写法

package com.bjpowernode.spring6.test;
 
import com.bjpowernode.spring6.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringJunit4Test {
    @Test
    public void testUser1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User user = applicationContext.getBean("user", User.class);
        System.out.println(user.getName());
    }
}

②使用Spring对Junit4的支持写法

(1)使用两个注解:

①@RunWith(SpringJUnit4ClassRunner.class),这个注解是junit里面的;

②@ContextConfiguration("classpath:spring.xml"),这个注解时Spring框架里面的;

使用这两个注解就相当于new ClassPathXmlApplicationContext("spring.xml");

(2)并且对于applicationContext.getBean("user", User.class);这段代码,我们可以先定义一个User属性,例如:private User user,然后使用@Autowired注解一注入即可

(3)所以我们以后在编写测试代码,如下:

package com.bjpowernode.spring6.test;
 
import com.bjpowernode.spring6.bean.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringJunit4Test {
 
    @Autowired
    private User user;
 
    @Test
    public void testUser2(){
        System.out.println(user.getName());
    }
}

执行结果

在JUnit4当中,Spring提供的方便主要是这几个注解:

①@RunWith(SpringJUnit4ClassRunner.class)
②@ContextConfiguration("classpath:spring.xml")

单元测试类上使用这两个注解之后,在单元测试类中的属性上可以使用@Autowired,比较方便!

2. Spring对JUnit5的支持

引入JUnit5的依赖,Spring对JUnit支持的依赖还是:spring-test,如下:

<!--junit5依赖-->
<dependency>
     <groupId>org.junit.jupiter</groupId>
     <artifactId>junit-jupiter</artifactId>
     <version>5.9.0</version>
     <scope>test</scope>
</dependency>

单元测试类

package com.bjpowernode.spring6.test;
 
import com.bjpowernode.spring6.bean.User;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
 
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringJunit5Test {
    @Autowired
    private User uer;
    @Test
    public void testUser(){
        System.out.println(uer.getName());
    }
}

在JUnit5当中,可以使用Spring提供的以下两个注解,标注到单元测试类上,这样在类当中就可以使用@Autowired注解了。

①@ExtendWith(SpringExtension.class)

②@ContextConfiguration("classpath:spring.xml")

总结:对于Spring对Junit4和Junit5的支持当中,代码主要有两点不同:

第一点:引入的注解不同

对于Junit4引入的一个注解是@RunWith(SpringJUnit4ClassRunner.class)

对于Junit5引入的一个注解时@ExtendWith(SpringExtension.class)

第二点:使用@Test注解的时导入的包不同

对于Junit4导入的包时org.junit.Test

对于Junit5导入的包时org.junit.jupiter.api.Test

以上就是Spring整合JUnit详解的详细内容,更多关于Spring整合JUnit的资料请关注脚本之家其它相关文章!

相关文章

  • java算法导论之FloydWarshall算法实现代码

    java算法导论之FloydWarshall算法实现代码

    这篇文章主要介绍了算法导论之FloydWarshall算法实现代码的相关资料,需要的朋友可以参考下
    2017-05-05
  • Spring Cloud详解实现声明式微服务调用OpenFeign方法

    Spring Cloud详解实现声明式微服务调用OpenFeign方法

    这篇文章主要介绍了Spring Cloud实现声明式微服务调用OpenFeign方法,OpenFeign 是 Spring Cloud 家族的一个成员, 它最核心的作用是为 HTTP 形式的 Rest API 提供了非常简洁高效的 RPC 调用方式,希望对大家有所帮助。一起跟随小编过来看看吧
    2022-07-07
  • Java8新特性之Stream使用详解

    Java8新特性之Stream使用详解

    这篇文章主要介绍了Java8新特性之Stream使用详解,流是用来处理集合中的数据,以声明的形式操作集合,它就像SQL语句,我们只需告诉流需要对集合进行什么操作,它就会自动进行操作,并将执行结果交给你,无需我们自己手写代码,需要的朋友可以参考下
    2023-08-08
  • Java用自定义的类作为HashMap的key值实例

    Java用自定义的类作为HashMap的key值实例

    下面小编就为大家带来一篇Java用自定义的类作为HashMap的key值实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • SpringBoot 集成 Druid过程解析

    SpringBoot 集成 Druid过程解析

    这篇文章主要介绍了SpringBoot 集成 Druid过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • MyBatis执行SQL的两种方式小结

    MyBatis执行SQL的两种方式小结

    本文主要介绍了MyBatis执行SQL的两种方式小结,主要包括SqlSession 发送SQL和SqlSession获取Mapper接口,通过Mapper接口发送SQL,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • 使用Java visualVM监控远程JVM的流程分析

    使用Java visualVM监控远程JVM的流程分析

    我们经常需要对我们的开发的软件做各种测试, 软件对系统资源的使用情况更是不可少,JDK1.6开始自带的VisualVM就是不错的监控工具,本文给大家分享使用Java visualVM监控远程JVM的问题,感兴趣的朋友跟随小编一起看看吧
    2021-05-05
  • SpringBoot如何进行对象复制的实践

    SpringBoot如何进行对象复制的实践

    本文主要介绍了SpringBoot 如何进行对象复制,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • java实现简单学生管理系统项目

    java实现简单学生管理系统项目

    这篇文章主要介绍了java实现简单学生管理系统项目,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • Java入门交换数组中两个元素的位置

    Java入门交换数组中两个元素的位置

    在Java中,交换数组中的两个元素是基本的数组操作,下面我们将详细介绍如何实现这一操作,以及在实际应用中这种技术的重要性,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09

最新评论