快速入门
添加依赖
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
创建测试目录
正式使用和启动类一样的目录
使用
java
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.danny.RuoYiDannyApplication;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
// 如果报错可以使用下面的直接导入:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
//@SpringBootTest
@SpringBootTest(classes = RuoYiDannyApplication.class)
@DisplayName("junit5功能测试,在idea下放显示自定义名称")
public class MyServiceTest {
// 测试代码
@Autowired
private RedisService redisService;
@Test
public void test1() {
System.out.println("测试1");
redisService.setCacheObject("hello", "world");
String hello = redisService.getCacheObject("hello");
System.out.println(hello);
}
@DisplayName("在idea下放显示自定义名称")
@Test
void testDisplayName() {
System.out.println("测试在idea下放显示自定义名称");
}
/**
* 测试前置条件
*/
@DisplayName("测试前置条件")
@Test
void testAssumptions() {
Assumptions.assumeTrue(false, "结果不足true");
System.out.println("11111");
}
@BeforeEach
void testBeforeEach() {
System.out.println("测试就要开始。。。");
}
@AfterEach
void testAfterEach() {
System.out.println("测试就要结束。。。");
}
@BeforeAll
static void testBeforeAll() {
System.out.println("所有测试就要开始。。。");
}
@AfterAll
static void testAfterAll() {
System.out.println("所有测试已经结束。。。");
}
}