【工具】Junit5单元测试工具

本文介绍了在SpringBoot2.4.0环境中使用Junit5进行单元测试的最佳实践,包括测试类的编写位置、pom.xml中的依赖配置,以及@BeforeEach、@BeforeAll、@AfterEach和@AfterAll等注解的使用。同时,还讨论了如何创建和执行测试套件来组织相关测试方法。
摘要由CSDN通过智能技术生成

1.环境

springboot2.4.0
Junit5

如果想改变spirngboot版本可修改2.4.0位置

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

2.写测试类的位置

在src-test文件夹下写测试类,打包部署时不会被编译
在这里插入图片描述

3.pom.xml中导入相关依赖依赖

springboot2.4.0 以上版本移除了默认对Vintage的依赖。如果需要兼容junit4需要自行引入

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

如需使用测试套件,需引入一下依赖

<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-runner</artifactId>
			<version>1.6.2</version>
		</dependency>

		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-launcher</artifactId>
			<version>1.6.2</version>
			<scope>test</scope>
		</dependency>

参数化的依赖

		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-params</artifactId>
			<version>5.6.2</version>
			<scope>test</scope>
		</dependency>

4.Junit5测试类最基础模板

(1)Junit4 需要加@Runwith注解
Junit5 不用加@Runwith注解:@ExtendWith注解代替了 @Runwith,而 @SpringBootTest 注解已经定义了 @ExtendWith的默认值
(2)@Test注解:标记方法为测试方法

package com.example;
//import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringbootDemoApplicationTests {

	@Test
	public void  a(){
      System.out.println ("a");
	}

}

5.注解(@BeforeEach、@BeforeAll、@AfterEach、@AfterAll)

(1)@BeforeEach:同一个类中每个测试方法执行前都会执行一次
在这里插入图片描述

package com.example;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class TestDemo1 {

    @BeforeEach
    void beforEachInit(){
        System.out.println("111111");
    }
    @Test
    public void a(){
        System.out.print("aa");
    }
    @Test
    public void b(){
        System.out.print("bb");
    }
    @Test
    public void c(){
        System.out.print("cc");
    }
}

(2)@BeforeAll:同一个类中所有测试方法执行之前执行一次,且被该注解修饰的方法必须为静态方法
在这里插入图片描述

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

//@SpringBootTest
public class TestDemo1 {

    @BeforeEach
    public void beforEachInit(){
        System.out.println("beforEach");
    }
    @BeforeAll
     public static  void beforeAllInit(){
        System.out.println("beforeAll");
    }
    @Test
    public void a(){
        System.out.print("aa");
    }
    @Test
    public void b(){
        System.out.print("bb");
    }
    @Test
    public void c(){
        System.out.print("cc");
    }
}

(3)@AfterEach:同一个类中每个测试方法执行之后都执行一次
(4)@AfterAll:同一个类中所有用例执行之后执行一次,且被该注解修饰的方法必须为静态方法
在这里插入图片描述

import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;

//@SpringBootTest
public class TestDemo1 {

    @BeforeEach
    public void beforEachInit(){
        System.out.println("beforEach");
    }
    @BeforeAll
     public static  void beforeAllInit(){
        System.out.println("beforeAll");
    }
    @Test
    public void a(){
        System.out.println("aa");
    }
    @Test
    public void b(){
        System.out.println("bb");
    }
    @AfterEach
    public void afterEach(){
        System.out.println("afterEach");
    }
    @AfterAll
    public static void afterAll(){
        System.out.println("afterAll");
    }
}

6.测试套件

概念:用来组织和运营一组相关的测试
(创建一个测试套件类,里面指定好可以执行哪些测试类。只启动这个套件类,指定的类中的方法便可执行)
在这里插入图片描述
测试套件类TotalDemo(配置了TestDemo2、TestDemo3测试方法)

package com.example;

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SelectClasses({TestDemo2.class, TestDemo3.class})
public class TotalDemo {

}

TestDemo2

package com.example;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
//@SpringBootTest
public class TestDemo2 {

    @Test
    public void testdemo2(){
        System.out.println("这是TestDemo2的方法");
    }
}

TestDemo3

package com.example;

import org.junit.jupiter.api.Test;
public class TestDemo3 {
    @Test
    public void testdemo3(){
        System.out.println("这是TestDemo3的方法");
    }
}

执行测试套件类TotalDemo
在这里插入图片描述

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

妄忧杂记

整理不易,请多多打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值