From 3e2f1549077eb14d43811802512d35a58c6d0aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 28 Oct 2022 23:59:13 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=A2=9E=E5=8A=A0=20junit5=20=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 13 ++++ ruoyi-example/ruoyi-demo/pom.xml | 6 ++ .../java/com/ruoyi/demo/AssertUnitTest.java | 45 ++++++++++++ .../java/com/ruoyi/demo/DemoUnitTest.java | 69 ++++++++++++++++++ .../java/com/ruoyi/demo/ParamUnitTest.java | 72 +++++++++++++++++++ .../test/java/com/ruoyi/demo/TagUnitTest.java | 54 ++++++++++++++ 6 files changed, 259 insertions(+) create mode 100644 ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/AssertUnitTest.java create mode 100644 ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/DemoUnitTest.java create mode 100644 ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/ParamUnitTest.java create mode 100644 ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/TagUnitTest.java diff --git a/pom.xml b/pom.xml index 8ab77382..d7d388ca 100644 --- a/pom.xml +++ b/pom.xml @@ -367,6 +367,19 @@ + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 + + -Dfile.encoding=UTF-8 + + ${profiles.active} + + exclude + + diff --git a/ruoyi-example/ruoyi-demo/pom.xml b/ruoyi-example/ruoyi-demo/pom.xml index 0b87a651..a7a737f8 100644 --- a/ruoyi-example/ruoyi-demo/pom.xml +++ b/ruoyi-example/ruoyi-demo/pom.xml @@ -101,6 +101,12 @@ com.ruoyi ruoyi-common-elasticsearch + + + org.springframework.boot + spring-boot-starter-test + test + diff --git a/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/AssertUnitTest.java b/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/AssertUnitTest.java new file mode 100644 index 00000000..9d20fb99 --- /dev/null +++ b/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/AssertUnitTest.java @@ -0,0 +1,45 @@ +package com.ruoyi.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** + * 断言单元测试案例 + * + * @author Lion Li + */ +@DisplayName("断言单元测试案例") +public class AssertUnitTest { + + @DisplayName("测试 assertEquals 方法") + @Test + public void testAssertEquals() { + Assertions.assertEquals("666", new String("666")); + Assertions.assertNotEquals("666", new String("666")); + } + + @DisplayName("测试 assertSame 方法") + @Test + public void testAssertSame() { + Object obj = new Object(); + Object obj1 = obj; + Assertions.assertSame(obj, obj1); + Assertions.assertNotSame(obj, obj1); + } + + @DisplayName("测试 assertTrue 方法") + @Test + public void testAssertTrue() { + Assertions.assertTrue(true); + Assertions.assertFalse(true); + } + + @DisplayName("测试 assertNull 方法") + @Test + public void testAssertNull() { + Assertions.assertNull(null); + Assertions.assertNotNull(null); + } + +} diff --git a/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/DemoUnitTest.java b/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/DemoUnitTest.java new file mode 100644 index 00000000..b186fd3c --- /dev/null +++ b/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/DemoUnitTest.java @@ -0,0 +1,69 @@ +package com.ruoyi.demo; + +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.concurrent.TimeUnit; + +/** + * 单元测试案例 + * + * @author Lion Li + */ +@SpringBootTest // 此注解只能在 springboot 主包下使用 需包含 main 方法与 yml 配置文件 +@DisplayName("单元测试案例") +public class DemoUnitTest { + + @Value("${spring.application.name}") + private String appName; + + @DisplayName("测试 @SpringBootTest @Test @DisplayName 注解") + @Test + public void testTest() { + System.out.println(appName); + } + + @Disabled + @DisplayName("测试 @Disabled 注解") + @Test + public void testDisabled() { + System.out.println(appName); + } + + @Timeout(value = 2L, unit = TimeUnit.SECONDS) + @DisplayName("测试 @Timeout 注解") + @Test + public void testTimeout() throws InterruptedException { + Thread.sleep(3000); + System.out.println(appName); + } + + + @DisplayName("测试 @RepeatedTest 注解") + @RepeatedTest(3) + public void testRepeatedTest() { + System.out.println(666); + } + + @BeforeAll + public static void testBeforeAll() { + System.out.println("@BeforeAll =================="); + } + + @BeforeEach + public void testBeforeEach() { + System.out.println("@BeforeEach =================="); + } + + @AfterEach + public void testAfterEach() { + System.out.println("@AfterEach =================="); + } + + @AfterAll + public static void testAfterAll() { + System.out.println("@AfterAll =================="); + } + +} diff --git a/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/ParamUnitTest.java b/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/ParamUnitTest.java new file mode 100644 index 00000000..ff224bef --- /dev/null +++ b/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/ParamUnitTest.java @@ -0,0 +1,72 @@ +package com.ruoyi.demo; + +import com.ruoyi.common.core.enums.UserType; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +/** + * 带参数单元测试案例 + * + * @author Lion Li + */ +@DisplayName("带参数单元测试案例") +public class ParamUnitTest { + + @DisplayName("测试 @ValueSource 注解") + @ParameterizedTest + @ValueSource(strings = {"t1", "t2", "t3"}) + public void testValueSource(String str) { + System.out.println(str); + } + + @DisplayName("测试 @NullSource 注解") + @ParameterizedTest + @NullSource + public void testNullSource(String str) { + System.out.println(str); + } + + @DisplayName("测试 @EnumSource 注解") + @ParameterizedTest + @EnumSource(UserType.class) + public void testEnumSource(UserType type) { + System.out.println(type.getUserType()); + } + + @DisplayName("测试 @MethodSource 注解") + @ParameterizedTest + @MethodSource("getParam") + public void testMethodSource(String str) { + System.out.println(str); + } + + public static Stream getParam() { + List list = new ArrayList<>(); + list.add("t1"); + list.add("t2"); + list.add("t3"); + return list.stream(); + } + + @BeforeEach + public void testBeforeEach() { + System.out.println("@BeforeEach =================="); + } + + @AfterEach + public void testAfterEach() { + System.out.println("@AfterEach =================="); + } + + +} diff --git a/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/TagUnitTest.java b/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/TagUnitTest.java new file mode 100644 index 00000000..37da80c2 --- /dev/null +++ b/ruoyi-example/ruoyi-demo/src/test/java/com/ruoyi/demo/TagUnitTest.java @@ -0,0 +1,54 @@ +package com.ruoyi.demo; + +import org.junit.jupiter.api.*; +import org.springframework.boot.test.context.SpringBootTest; + +/** + * 标签单元测试案例 + * + * @author Lion Li + */ +@SpringBootTest +@DisplayName("标签单元测试案例") +public class TagUnitTest { + + @Tag("dev") + @DisplayName("测试 @Tag dev") + @Test + public void testTagDev() { + System.out.println("dev"); + } + + @Tag("prod") + @DisplayName("测试 @Tag prod") + @Test + public void testTagProd() { + System.out.println("prod"); + } + + @Tag("local") + @DisplayName("测试 @Tag local") + @Test + public void testTagLocal() { + System.out.println("local"); + } + + @Tag("exclude") + @DisplayName("测试 @Tag exclude") + @Test + public void testTagExclude() { + System.out.println("exclude"); + } + + @BeforeEach + public void testBeforeEach() { + System.out.println("@BeforeEach =================="); + } + + @AfterEach + public void testAfterEach() { + System.out.println("@AfterEach =================="); + } + + +}