Spring——整合junit4、junit5使用方法

单元测试是测试单元代码

junit4

流程

  • 添加JUnit 4依赖:在项目的构建工具(如Maven或Gradle)的配置文件中,添加JUnit 4的依赖。
  • 创建测试类:创建一个普通的Java类,并在类上添加@RunWith(SpringJUnit4ClassRunner.class)注解。
  • 添加Spring配置:在测试类中,使用@ContextConfiguration注解指定Spring的配置文件或配置类。
  • 编写测试方法:在测试类中,使用@Test注解标记测试方法,并编写测试逻辑。
  • 运行测试:使用IDE或构建工具运行测试类,观察测试结果。

依赖

<?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">
    <parent>
        <artifactId>02-Spring</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>023-jUnit</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>6.0.4</version>
        </dependency>
    </dependencies>
</project>

配置文件


<?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.powernode.spring6.bean"/>
</beans>

实体类


package com.powernode.spring6.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.powernode.spring6.bean
 * @Author: dengLiMei
 * @CreateTime: 2023-02-21  09:07
 * @Description: TODO
 * @Version: 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;
    }
}

主函数

没有使用junit4之前

public class SpringJunit4Test {
    @Test
    public void testUser(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.getName());
    }

    public void testUser1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.getName());
    }

    public void testUser2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.getName());
    }
}
}
使用junit4之后

//Spring对Junit4的支持
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringJunit4Test {
    @Autowired
    private User user;

    @Test
    public void testUser() {
        System.out.println(user.getName());
    }

    public void testUser1() {
        System.out.println(user.getName());
    }

    public void testUser2() {
        System.out.println(user.getName());
    }

通过一张图我们来对比一下更直观,如下图:

在上面的示例中,User 是要测试的Spring组件。通过使用@Autowired注解,我们可以将该组件注入到测试类中,以便在测试中使用。

然后,我们可以在testUser方法中编写测试逻辑。可以调用testUser的方法,并使用断言来验证方法的行为和结果是否符合预期。

最后,我们可以运行这个测试类,JUnit 4会加载Spring配置文件,并执行测试方法。

这只是一个简单的示例,实际的测试场景可能更复杂。你可以根据自己的需求来编写更多的测试方法,并使用JUnit提供的各种断言和注解来辅助测试。


junit5

流程

  • 添加JUnit 5依赖:在项目的构建工具(如Maven或Gradle)的配置文件中,添加JUnit 5的依赖。
  • 创建测试类:创建一个普通的Java类,并在类上添加@ExtendWith(SpringExtension.class)注解。
  • 添加Spring配置:在测试类中,使用@ContextConfiguration注解指定Spring的配置文件或配置类。
  • 编写测试方法:在测试类中,使用@Test注解标记测试方法,并编写测试逻辑。
  • 运行测试:使用IDE或构建工具运行测试类,观察测试结果。

依赖

<?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">
    <parent>
        <artifactId>02-Spring</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>023-jUnit5</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>6.0.4</version>
        </dependency>
    </dependencies>
</project>

配置文件


<?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="bean"/>
</beans>

实体类


package bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.powernode.spring6.bean
 * @Author: dengLiMei
 * @CreateTime: 2023-02-21  09:07
 * @Description: TODO
 * @Version: 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;
    }
}

主函数


import 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;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: PACKAGE_NAME
 * @Author: dengLiMei
 * @CreateTime: 2023-02-21  11:18
 * @Description: TODO
 * @Version: 1.0
 */
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:spring.xml")
public class Main {


    @Autowired
    private User user;

    @Test
    public void testUser() {
        System.out.println(user.getName());
    }

    public void testUser1() {
        System.out.println(user.getName());
    }

    public void testUser2() {
        System.out.println(user.getName());
    }
}

在上面的示例中,User是要测试的Spring组件。通过使用@Autowired注解,我们可以将该组件注入到测试类中,以便在测试中使用。

然后,我们可以在testUser方法中编写测试逻辑。使用断言来验证方法的行为和结果是否符合预期。

最后,我们可以运行这个测试类,JUnit 5会加载Spring配置文件,并执行测试方法。

JUnit 5还提供了更多的功能,如参数化测试、动态测试、条件测试等。你可以根据自己的需求来编写更多的测试方法,并使用JUnit提供的各种注解和断言来辅助测试。

总结

无论是JUnit 4还是JUnit 5,都可以使用Spring的依赖注入、事务管理等特性进行单元测试。需要注意的是,JUnit 5相对于JUnit 4有更多的新特性和改进,例如支持Lambda表达式、扩展模型等。因此,如果是新项目或者可以升级到JUnit 5的情况下,推荐使用JUnit 5进行单元测试。


Spring系列文章:

Spring——是什么?作用?内容?用到的设计模式?

Spring——Bean管理-xml方式进行属性注入

Spring——Bean管理-注解方式进行属性注入

Spring——什么是IOC?

Spring——AOP是什么?如何使用?

Spring——什么是事务?传播行为?事务隔离级别有哪些?

Spring——整合junit4、junit5使用方法

如果有想要交流的内容欢迎在评论区进行留言,如果这篇文档受到了您的喜欢那就留下你点赞+收藏脚印支持一下博主~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小乔努力变强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值