当前位置:网站首页>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 ");
}
}边栏推荐
- Not so large number of combinations
- php存储密码
- 总结的太好了!终于有人把SQL的各种连接Join都讲明白了
- Solution to sudo PIP install gevent installation failure
- SPARK中的FileSourceStrategy,DataSourceStrategy以及DataSourceV2Strategy
- Deep learning 7 deep feedforward network 2
- Stm32subeide (9) -- USART sends and receives via DMA
- 使用toruch.nn搭建最简单的神经网络骨架
- 全志V3s学习记录(13)OV2640的使用
- Redis overview installation
猜你喜欢
随机推荐
深度学习第三周Shallow Neural Networks习题整理
5.1 安全漏洞与防范
Softmax 回归 + 损失函数 + 图片分类数据集
OpenFeign服务接口调用
Redis common data types - redis list and redis set
Do online usdt and usdc want to be short? Take you to find out | tokenview
WVPPRO-ZLM-GB21818-摄像头
Redis publishing and subscription
Unity: WebGL发布后在浏览器上运行时窗口大小自适应
trochvision中数据集的使用
网传USDT和USDC要做空?带你一探究竟 | Tokenview
Redis6 new data type - hyperloglog
使用arduino开发esp8266和esp32时首选项设置方法
巴西移动游戏代投出海机遇与挑战
How to select MCU?
Deep learning 7 deep feedforward network
JS learning notes 06-08: traversal of arrays and four methods of arrays
1. Flask Foundation
oop_ Reference type variable transfer value
Oi memoirs









