当前位置:网站首页>Junit5
Junit5
2022-07-17 06:43:00 【仰望星空的快乐】
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>如果是junit5,则使用的@Test注解是在import org.junit.jupiter.api.Test;类中的。
@Transactional
可以使用@Transactional 给方法添加事务,虽然返回值是true,但是数据库中并不减少数据

@Test
@Transactional
public void testusers()
{
System.out.println(userServiceimpl.removeById(1));
}@displayname
@DisplayName("测试类1")
public class test {
@Autowired
public UserServiceimpl userServiceimpl;
@Test
@Transactional
@DisplayName("测试类1的测试方法1")
public void testusers()
{
System.out.println(userServiceimpl.removeById(2));
}
}
点击类上的运行,可以运行这个类的所有测试方法

@beforeall @afterall 在所有测试方法执行前或者后执行一次
@beforeeeach @aftereach 在每个测试方法执行前或者执行后执行
@BeforeAll
public static void beforeall()
{
System.out.println("在所有的测试方法前执行");
}
@AfterAll
public static void afterall()
{
System.out.println("在所有的测试方法后执行");
}
@BeforeEach
public void beforeeach()
{
System.out.println("在每个测试方法前执行-----");
}
@AfterEach
public void aftereach()
{
System.out.println("在每个测试方法后执行+++++");
}
@Disabled 在执行测试类的测试时,禁止执行该方法
@Test
@Disabled
@DisplayName("测试类1的测试方法1")
public void test1()
{
System.out.println("测试方法一");
}@Timeout等待多长时间,如果到达时间,还没执行完,就抛出异常
@Test
@DisplayName("测试类1的测试方法1")
@Timeout(value = 500,unit = TimeUnit.MILLISECONDS)
public void test1() throws Exception
{
Thread.sleep(5000);//设置说5000毫秒,让程序超时
System.out.println("测试方法一");
}断言,判断结果和预期是否一致,不一致就报错,一致就没事
// 前面的断言失败,后面的代码都不会执行
@DisplayName("简单断言测试")
@Test
public void test()
{
// 第一个是希望的,第二个是实际的,第三个是显示的提示
assertEquals(5,4,"业务逻辑");
Object o1=new Object();
Object o2=new Object();
// 判断两个对象是不是相等,第三个还是显示的提示
assertSame(o1,o2);
}判断数组是否一致
@Test
@DisplayName("断言测试数组")
void array()
{
assertArrayEquals(new int[]{1,2},new int[]{1,3});
}会告诉哪个地方不对,应该是什么

组合断言 只有全部通过才算通过
@Test
@DisplayName("组合断言")
public void all(){
assertAll("组合断言",()-> assertTrue(true),()->assertEquals(1,2));
}异常断言 当代码没有抛出异常时报错,当抛出了异常不报错
@DisplayName("异常断言")
@Test
public void testexception()
{
assertThrows(ArithmeticException.class,()->{int i=10/2;},"message代码没有抛出异常!!!");
}
快速停止 如果满足条件,就停止调试,它后面的代码都不执行了
@DisplayName("快速失败")
@Test
public void testfail()
{
if(1==1)
{
fail("测试失败");
}
}边栏推荐
- Shenzhen Prudential written examination record
- A set of Jenkins style for personal use
- Jira --- workflow call external api
- Pytoch notes (3)
- 在线问题反馈模块实战(五):实现对通用字段内容自动填充功能
- 力扣114题:二叉树展开链表
- Niuke topic - house raiding 1, house raiding 2
- 4-channel fmc+ baseband signal processing board (4-channel 2G instantaneous bandwidth ad+da)
- Xilinx ultrascale+ MPSoC (zu9eg/zu15eg) high performance PCIe data preprocessing board
- [C console] - C console class
猜你喜欢

VMware Cloud Director 10.4 发布 (含下载) - 云计算调配和管理平台

从赌场逻辑,分析平台币的投资价值 2020-03-03
![[JVM] heap memory, escape analysis, on stack allocation, synchronous omission, scalar replacement details](/img/cd/b359a746da9e411590927963c0d2a7.png)
[JVM] heap memory, escape analysis, on stack allocation, synchronous omission, scalar replacement details

CCF-CSP《202206-2—寻宝!大冒险!》

Convolutional neural network CNN

Jira --- workflow call external api

Modify the select style

Mongodb index

Question 114 of Li Kou: binary tree expansion linked list
![[C language] user defined type details: structure, enumeration, union](/img/5c/69d238a71e24b34c2232c20ed7e09e.png)
[C language] user defined type details: structure, enumeration, union
随机推荐
What if the user information in the website app database is leaked and tampered with
Local storage sessionstorage
在线问题反馈模块实战(五):实现对通用字段内容自动填充功能
Wechat oauth2.0 login process and security analysis
RISC-V技術雜談
2022 review questions and mock exams for main principals of hazardous chemical business units
Redis jump table implementation principle & time complexity analysis
DP动态规划企业级模板分析(数字三角,上升序列,背包,状态机,压缩DP)
[MySQL] lock mechanism: detailed explanation of lock classification, table lock, row lock and page lock in InnoDB engine
Visual Studio 生产环境配置方案:SlowCheetah
the max_ iter was reached which means the coef_ did not converge “the coef_ did not converge“
redis集群
Flutter3.0(framework框架)——UI渲染
Practice of online problem feedback module (V): realize the automatic filling function of general field content
Regular expression extraction of matching content
Standard Version (release and changelog Automation)
Double index mechanism of redis source code analysis
4-channel FMC interface baseband signal processing board (2 FMC interfaces, 2 fmc+ interfaces)
Use of fiddler packet capturing tool
Leetcode daily question 2021/7/11-2021/7/17