【Spring6】| Spring6 integrates JUnit

Table of contents

One: Spring6 integrates JUnit

1. Spring's support for JUnit4

2. Spring's support for JUnit5


One: Spring6 integrates JUnit

1. Spring's support for JUnit4

Preparations: pom.xml

Note: In the past, Junit was directly used for unit testing, but now Spring's integration of Junit is used!

<?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>

Declare 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 configuration

<?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>

unit test:

①The previous way of writing

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

②Using Spring's support for Junit4

(1) Use two annotations:

@RunWith (SpringJUnit4ClassRunner.class), this annotation is in junit;

@ContextConfiguration ("classpath:spring.xml"), this annotation is in the Spring framework;

Using these two annotations is equivalent to new ClassPathXmlApplicationContext("spring.xml");

(2) And for applicationContext.getBean("user", User.class); this code, we can first define a User attribute, for example: private User user, and then use the @Autowired annotation to inject it

(3) So we will write test code in the future, as follows:

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

Results of the

In JUnit4, the convenience provided by Spring is mainly these annotations:

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

After using these two annotations on the unit test class, @Autowired can be used on the properties in the unit test class, which is more convenient!

2. Spring's support for JUnit5

Introducing the dependency of JUnit5, Spring's dependency on JUnit support is still: spring-test, as follows:

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

unit test class

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

In JUnit5, you can use the following two annotations provided by Spring to annotate the unit test class, so that you can use the @Autowired annotation in the class.

①@ExtendWith(SpringExtension.class)

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

Summary: For Spring's support for Junit4 and Junit5, there are two main differences in the code:

The first point: the introduced annotations are different

An annotation introduced for Junit4 is @RunWith(SpringJUnit4ClassRunner.class)

For an annotation introduced by Junit5 @ExtendWith(SpringExtension.class)

The second point: the imported package is different when using the @Test annotation

For packages imported by Junit4 org.junit.Test

For packages imported by Junit5 org.junit.jupiter.api.Test

Guess you like

Origin blog.csdn.net/m0_61933976/article/details/128761020