单元测试spring-boot-starter-test

参考博客: https://www.cnblogs.com/mzc1997/p/14306538.html

配置pom

junit-vintage-engine junit4

junit-jupiter-engine junit5

排除junit4使用junit5,两者在切换时要特别注意

pom 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
	<exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
	<groupId>org.junit.jupiter</groupId>
	<artifactId>junit-jupiter-engine</artifactId>
</dependency>

命名规范

单元测试类的命名规范为:被测试类的类名+Test。.

单元测试类中测试方法的命名规范为:test+被测试方法的方法名+AAA,其中AAA为对同一个方法的不同的单元测试用例的自定义名称。

junit5的使用

java 复制代码
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DisplayName("Base Test junit5才有") // 显示名称通常用于IDE和构建中的测试报告
class DemoFlowfestApplicationTests {
	@BeforeAll
	public static void beforall(){
		System.out.println("beforall***");
	}

	@Test
	void contextLoads() {
		System.out.println("run___1");
	}
	
    @Test
    void contextLoads2() {
        System.out.println("run___2");
    }

	@AfterAll
	public static void afterall(){
		System.out.println("after****");
	}

}
beforall***
run___2
run___1
after****

@ActiveProfiles("test")

需要配置resources

junit4

java 复制代码
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
// 让 JUnit 运行 Spring 的测试环境, 获得 Spring 环境的上下文的支持
@RunWith(SpringRunner.class)
// 获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类(被 @SpringBootApplication 注解的)
@SpringBootTest(value={"com.example.demoflowfest.DemoFlowfestApplication"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//这里一定要有public
public class DemoFlowfestApplicationTestsTwo {
	@Before
	public void befer(){
		System.out.println("befer***");
	}

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

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

	@After
	public void after(){
		System.out.println("after**");
	}
}
返回值
befer***
run___bb
after**
befer***
run___aa
after**
相关推荐
云动雨颤2 天前
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
python·单元测试
Suresoft China2 天前
软件测试|STATIC 代码静态验证工具 C/C++ 工具链设置指南
c++·单元测试·静态测试·测试覆盖率·static·代码覆盖率·工具链设置
itppxie3 天前
Simulink中使用Test sequence单元测试
单元测试
泛联新安4 天前
如何根据项目需求选择合适的软件测试工具?iUnit智能单元测试平台提供专业化解决方案
c++·测试工具·单元测试
EndingCoder4 天前
单元测试:Jest 与 Electron 的结合
javascript·electron·单元测试·前端框架
奔跑吧邓邓子5 天前
【Java实战㉖】深入Java单元测试:JUnit 5实战指南
java·junit·单元测试·实战·junit5
川石课堂软件测试6 天前
Oracle 数据库使用事务确保数据的安全
数据库·python·功能测试·docker·oracle·单元测试·prometheus
程序员二黑6 天前
Postman接口测试全攻略:从入门到精通,看这一篇就够了
单元测试·测试·ab测试
OEC小胖胖7 天前
代码质量保障:使用Jest和React Testing Library进行单元测试
前端·react.js·单元测试·前端框架·web