• Spring6整合JUnit5


    十七、Spring6整合JUnit5

    17.1 Spring对JUnit4的支持

    准备工作:

    
    <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.0modelVersion>
    
        <groupId>com.powernodegroupId>
        <artifactId>spring6-015-junitartifactId>
        <version>1.0-SNAPSHOTversion>
        <packaging>jarpackaging>
    
        
        <repositories>
            
            <repository>
                <id>repository.spring.milestoneid>
                <name>Spring Milestone Repositoryname>
                <url>https://repo.spring.io/milestoneurl>
            repository>
        repositories>
    
        <dependencies>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>6.0.0-M2version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>6.0.0-M2version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13.2version>
                <scope>testscope>
            dependency>
        dependencies>
    
        <properties>
            <maven.compiler.source>17maven.compiler.source>
            <maven.compiler.target>17maven.compiler.target>
        properties>
    
    project>
    package com.powernode.spring6.bean;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * @author 动力节点
     * @version 1.0
     * @className User
     * @since 1.0
     **/
    @Component
    public class User {
    
        @Value("张三")
        private String name;
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public User() {
        }
    
        public User(String name) {
            this.name = name;
        }
    }
    
    <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.powernode.spring6.bean"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    单元测试

    package com.powernode.spring6.test;
    
    import com.powernode.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;
    
    /**
     * @author 动力节点
     * @version 1.0
     * @className SpringJUnit4Test
     * @since 1.0
     **/
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:spring.xml")
    public class SpringJUnit4Test {
    
        @Autowired
        private User user;
    
        @Test
        public void testUser(){
            System.out.println(user.getName());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    执行结果如下:

    img

    Spring提供的方便主要是这几个注解:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(“classpath:spring.xml”)

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

    17.2 Spring对JUnit5的支持

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

    <?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.powernode</groupId>
        <artifactId>spring6-015-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>
                <version>6.0.0-M2</version>
            </dependency>
            <!--junit5依赖-->
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter</artifactId>
                <version>5.9.0</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <properties>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
        </properties>
    
    </project>
    package com.powernode.spring6.test;
    
    import com.powernode.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 user;
    
        @Test
    //这个需要注意,是import org.junit.jupiter.api.Test;里面的Test,不是org中的,引错的话会出现空指针异常
        public void testUser(){
            System.out.println(user.getName());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

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

    @ExtendWith(SpringExtension.class)

    @ContextConfiguration(“classpath:spring.xml”)

    以上内容来自于动力节点老杜的spring6课程
    b站动力节点spring6的spring6整合junit

  • 相关阅读:
    CentorOS上安装elasticsearch7.17.3,不用docker方式
    Docker镜像的拉取和推送(阿里云镜像仓库)
    rsync 远程同步
    JS迭代器及异步
    关于手机上的卫星定位
    【C++】详细讲解指针使用,带你玩转C++指针~
    compact unwind compressed function offset doesn‘t fit in 24 bits
    车载通信与DDS标准解读系列(1):DDS-RPC
    使用Django框架快速搭建个人网站
    AWS上迁移WordPress遭遇若干问题记处理办法
  • 原文地址:https://blog.csdn.net/CXYCMY/article/details/127927561