当前位置:网站首页>Junit5
Junit5
2022-07-19 08:33:00 【The joy of looking up at the starry sky】
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>If it is junit5, Then use @Test The note is in import org.junit.jupiter.api.Test; Class .
@Transactional
have access to @Transactional Add transaction to method , Although the return value is true, But there is no reduction of data in the database

@Test
@Transactional
public void testusers()
{
System.out.println(userServiceimpl.removeById(1));
}@displayname
@DisplayName(" Test class 1")
public class test {
@Autowired
public UserServiceimpl userServiceimpl;
@Test
@Transactional
@DisplayName(" Test class 1 The test method of 1")
public void testusers()
{
System.out.println(userServiceimpl.removeById(2));
}
}
Click Run on the class , You can run all the test methods of this class

@beforeall @afterall Execute once before or after the execution of all test methods
@beforeeeach @aftereach Execute before or after the execution of each test method
@BeforeAll
public static void beforeall()
{
System.out.println(" Execute before all test methods ");
}
@AfterAll
public static void afterall()
{
System.out.println(" Execute after all test methods ");
}
@BeforeEach
public void beforeeach()
{
System.out.println(" Execute... Before each test method -----");
}
@AfterEach
public void aftereach()
{
System.out.println(" Execute after each test method +++++");
}
@Disabled When executing the test of the test class , It is forbidden to execute this method
@Test
@Disabled
@DisplayName(" Test class 1 The test method of 1")
public void test1()
{
System.out.println(" Test method 1 ");
}@Timeout How long to wait , If the arrival time , We're not done yet , Throw an exception
@Test
@DisplayName(" Test class 1 The test method of 1")
@Timeout(value = 500,unit = TimeUnit.MILLISECONDS)
public void test1() throws Exception
{
Thread.sleep(5000);// Setting theory 5000 millisecond , Let the program time out
System.out.println(" Test method 1 ");
}Assertion , Judge whether the result is consistent with the expectation , If you don't agree, you report an error , It's ok if you agree
// The previous assertion failed , None of the following code will execute
@DisplayName(" Simple assertion tests ")
@Test
public void test()
{
// The first is hope , The second is practical , The third is the displayed prompt
assertEquals(5,4," Business logic ");
Object o1=new Object();
Object o2=new Object();
// Judge whether two objects are equal , The third is the displayed prompt
assertSame(o1,o2);
}Determine whether the array is consistent
@Test
@DisplayName(" Assert test array ")
void array()
{
assertArrayEquals(new int[]{1,2},new int[]{1,3});
}Will tell which place is wrong , What should be

Combine assertions Only if all of them are passed
@Test
@DisplayName(" Combine assertions ")
public void all(){
assertAll(" Combine assertions ",()-> assertTrue(true),()->assertEquals(1,2));
}Exception assertion When the code does not throw an exception, an error is reported , When an exception is thrown, no error is reported
@DisplayName(" Exception assertion ")
@Test
public void testexception()
{
assertThrows(ArithmeticException.class,()->{int i=10/2;},"message The code doesn't throw an exception !!!");
}
Stop quickly If the conditions are met , Stop debugging , The code behind it is not executed
@DisplayName(" Fast failure ")
@Test
public void testfail()
{
if(1==1)
{
fail(" Test to fail ");
}
}边栏推荐
- Openpyxl copy sheet pages across workbooks
- Talk about distributed locks
- 使用arduino开发esp8266和esp32时首选项设置方法
- JS学习笔记06-08:数组的遍历以及数组的四个方法
- Consul服务注册与发现
- With this "programmer code interview guide" from Zuo Chengyun (Zuo Shen), I joined byte
- WVPPRO-ZLM-GB21818-摄像头
- WebGL的CPU负载问题-WebGL对比
- 812. Maximum triangle area
- PHP store password
猜你喜欢

力扣1669合并两个链表笔记

Enjoy JVM -- knowledge about GC garbage collection

Microservices and microservice architecture

百度Apoll

Classic general pbootcms flower website template source code, adaptive mobile terminal, with background management

1. Flask Foundation

半透明双层玻璃侧厚

Gateway new generation gateway

Application of SCA on devsecops platform

US pressure surges, tiktok changes global safety director
随机推荐
60. Initial knowledge of wsgiref handwritten web framework +jinja2 module
Redis6 new data type - hyperloglog
Redis common data types - hash and ordered set Zset (sorted set)
OpenFeign服务接口调用
#yyds干货盘点#Cross-origin 跨域请求
力扣455分发饼干笔记
Consul service registration and discovery
Seaport 以及 ERC-4907 能否成为释放NFT流动性的新途径?| Tokenview
Unity: WebGL发布后在浏览器上运行时窗口大小自适应
Demo collection injection
JS learning notes 14-15: JS array and array letter quantity
1、flask基础
Eureka self protection
力扣382链表随机节点笔记
C # read and write txt files
凭借左程云(左神)的这份 “程序员代码面试指南”我入职了字节
Spark miscellaneous -- why reuse exchange and subquery
使用arduino开发esp8266和esp32时首选项设置方法
Enjoy JVM -- knowledge about GC garbage collection
3D laser slam:aloam --- interpretation of inter frame odometer code