java SpringBoot(十五)JUnit5简单使用

JUnit5的注解与JUnit4的注解有所变化

https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations

  • @Test :表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试
  • @ParameterizedTest :表示方法是参数化测试,下方会有详细介绍
  • @RepeatedTest :表示方法可重复执行,下方会有详细介绍
  • @DisplayName :为测试类或者测试方法设置展示名称
  • @BeforeEach :表示在每个单元测试之前执行
  • @AfterEach :表示在每个单元测试之后执行
  • @BeforeAll :表示在所有单元测试之前执行
  • @AfterAll :表示在所有单元测试之后执行
  • @Tag :表示单元测试类别,类似于JUnit4中的@Categories
  • @Disabled :表示测试类或测试方法不执行,类似于JUnit4中的@Ignore
  • @Timeout :表示测试方法运行如果超过了指定时间将会返回错误
  • @ExtendWith :为测试类或测试方法提供扩展类引用
package com.example.demo;

import com.example.demo.bean.User;
import com.example.demo.mapper.UserMapper;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

@SpringBootTest //可以使用容器中的内容
class MysqlTestApplicationTests {

	@Autowired
	UserMapper userMapper;

	@RepeatedTest(5) //重复测试五次
	@Disabled //禁用该测试
	@Timeout(value = 5, unit = TimeUnit.SECONDS) //超过5s 就是超时
	@DisplayName("usermapper test") //该测试的名称
	@Test
	void testUserMapper(){
		User user = userMapper.selectById(1L);
		System.out.println(user.toString());
	}

	@Autowired
	StringRedisTemplate stringRedisTemplate;

	@DisplayName("redis test")
	@Test
	void testRedis(){
		stringRedisTemplate.opsForValue().set("aaa","1");
		System.out.println(stringRedisTemplate.opsForValue().get("aaa"));
	}

	//在每个测试开始前
	@BeforeEach
	void testBeforEach(){
		System.out.println("start test");
	}

	//在每个测试开始后
	@AfterEach
	void testAfterEach(){
		System.out.println("end test");
	}

	//在所有测试开始之前执行  用于开启某些连接
	@BeforeAll
	static void testBeforAll(){
		System.out.println("start all test");
	}

	//在所有测试开始之后  用于关闭某些连接
	@AfterAll
	static void testAfterAll(){
		System.out.println("after all test");
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值