Skip to content

Commit e00b9b1

Browse files
committed
202109021435 增加 junit-practice 模块
1 parent 12eb71a commit e00b9b1

File tree

11 files changed

+450
-1
lines changed

11 files changed

+450
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ hs_err_pid*
2525

2626
*.idea
2727
.idea
28-
.idea/
28+
.idea/
29+
30+
target
31+
target/

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
11
# Java 架构师的工具箱
22

33

4+
5+
6+
## 项目1:junit5-practice
7+
8+
### 参考文档
9+
10+
- [Idea-Maven多模块开发](https://www.jianshu.com/p/274455dc9469)
11+
- [idea报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile](https://blog.csdn.net/gao_jun1/article/details/109997200)
12+
- [junit5-examples](https://github.com/mkyong/junit5-examples)
13+
- [该升级你的JUnit版本了——JUnit5基本介绍](https://zhuanlan.zhihu.com/p/111706639)
14+
- [Java单元测试之JUnit 5快速上手【实践代码】](https://www.cnblogs.com/one12138/p/11536492.html)
15+
- [junit 5中三种不同指定用例测试顺序](https://blog.csdn.net/jackyrongvip/article/details/89389387)
16+
17+
18+
19+
20+
21+
## TODO
22+
23+
- SendMail
24+
- 解析Excel
25+
- Swagger
26+
- sql过滤模糊查询条件中的特殊字符
27+
28+
29+
30+
31+
32+
33+
34+
# 其他参考文档
35+
36+
- [IDEA如何设置author头注解](https://blog.csdn.net/weixin_42555514/article/details/106826894)
37+
38+
39+
40+
# END

junit5-practice/pom.xml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.coderdream</groupId>
8+
<artifactId>junit5-practice</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>junit5-practice</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
16+
<properties>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<junit-jupiter.version>5.5.2</junit-jupiter.version>
20+
<junit-jupiter-params>5.5.2</junit-jupiter-params>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
</properties>
23+
24+
<dependencies>
25+
26+
<dependency>
27+
<groupId>org.junit.jupiter</groupId>
28+
<artifactId>junit-jupiter-engine</artifactId>
29+
<version>${junit-jupiter.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter</artifactId>
35+
<version>${junit-jupiter.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.junit.jupiter</groupId>
41+
<artifactId>junit-jupiter-params</artifactId>
42+
<version>${junit-jupiter-params}</version>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
51+
<!-- Need at least 2.22.0 to support JUnit 5 platform -->
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-surefire-plugin</artifactId>
55+
<version>3.0.0-M3</version>
56+
</plugin>
57+
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<version>3.8.1</version>
62+
<configuration>
63+
<source>1.8</source>
64+
<target>1.8</target>
65+
</configuration>
66+
</plugin>
67+
68+
<!-- optional, for mvn site -->
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-site-plugin</artifactId>
72+
<version>3.8.2</version>
73+
</plugin>
74+
75+
<!--
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-resources-plugin</artifactId>
79+
<version>3.1.0</version>
80+
<configuration>
81+
<encoding>UTF-8</encoding>
82+
</configuration>
83+
</plugin>
84+
-->
85+
86+
</plugins>
87+
</build>
88+
89+
<!-- optional, add surefire report into the mvn site -->
90+
<reporting>
91+
<plugins>
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-surefire-report-plugin</artifactId>
95+
</plugin>
96+
</plugins>
97+
</reporting>
98+
99+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderdream;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coderdream;
2+
3+
//import static org.junit.Assert.assertTrue;
4+
//
5+
//import org.junit.Test;
6+
7+
//import org.junit.Test;
8+
9+
/**
10+
* Unit test for simple App.
11+
*/
12+
public class AppTest
13+
{
14+
/**
15+
* Rigorous Test :-)
16+
*/
17+
// @Test
18+
// public void shouldAnswerWithTrue()
19+
// {
20+
// assertTrue( true );
21+
// }
22+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.coderdream;
2+
3+
4+
import org.junit.jupiter.api.*;
5+
6+
/**
7+
* @author :CoderDream
8+
* @date :Created in 2021/9/2 10:02
9+
* @description:我的第一个测试用例
10+
* @modified By:CoderDream
11+
* @version: 1.0$
12+
*/
13+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
14+
public class MyFirstTestCaseTest {
15+
16+
@BeforeAll
17+
public static void init() {
18+
System.out.println("初始化数据");
19+
}
20+
21+
@AfterAll
22+
public static void cleanUp() {
23+
System.out.println("清理数据");
24+
}
25+
26+
@BeforeEach
27+
public void tearUp() {
28+
System.out.println("当前测试方法开始");
29+
}
30+
31+
@AfterEach
32+
public void tearDown() {
33+
System.out.println("当前测试方法结束");
34+
}
35+
36+
@DisplayName("我的第一个测试")
37+
@Order(1)
38+
@Test
39+
void testFirstTest() {
40+
System.out.println("我的第一个测试开始测试");
41+
}
42+
43+
@DisplayName("我的第二个测试")
44+
@Order(2)
45+
@Test
46+
void testSecondTest() {
47+
System.out.println("我的第二个测试开始测试");
48+
}
49+
50+
@DisplayName("我的第三个测试")
51+
@Disabled
52+
@Order(3)
53+
@Test
54+
void testThirdTest() {
55+
System.out.println("我的第三个测试开始测试");
56+
}
57+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.coderdream;
2+
3+
import org.junit.jupiter.api.*;
4+
5+
/**
6+
* @author :CoderDream
7+
* @date :Created in 2021/9/2 11:22
8+
* @description:内嵌测试类
9+
* @modified By:CoderDream
10+
* @version: 1.0$
11+
*/
12+
@DisplayName("内嵌测试类")
13+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
14+
public class NestUnitTest {
15+
@BeforeEach
16+
void init() {
17+
System.out.println("测试方法执行前准备");
18+
}
19+
20+
@Nested
21+
@DisplayName("第一个内嵌测试类")
22+
class FirstNestTest {
23+
@Test
24+
@Order(1)
25+
void test() {
26+
System.out.println("第一个内嵌测试类执行测试");
27+
}
28+
}
29+
30+
@Nested
31+
@DisplayName("第二个内嵌测试类")
32+
class SecondNestTest {
33+
@Test
34+
@Order(2)
35+
void test() {
36+
System.out.println("第二个内嵌测试类执行测试");
37+
}
38+
}
39+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.coderdream;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.time.Duration;
8+
import java.time.temporal.ChronoUnit;
9+
10+
/**
11+
* @author :CoderDream
12+
* @date :Created in 2021/9/2 11:46
13+
* @description:新的断言
14+
* @modified By:CoderDream
15+
* @version: 1.0$
16+
*/
17+
public class NewAssertionsUnitTest {
18+
@Test
19+
void testGroupAssertions() {
20+
int[] numbers = {0, 1, 2, 3, 4};
21+
Assertions.assertAll("numbers",
22+
() -> Assertions.assertEquals(numbers[1], 1),
23+
() -> Assertions.assertEquals(numbers[3], 3),
24+
() -> Assertions.assertEquals(numbers[4], 4)
25+
);
26+
}
27+
28+
// @Test
29+
// @DisplayName("超时方法测试")
30+
// void test_should_complete_in_one_second() {
31+
// Assertions.assertTimeoutPreemptively(Duration.of(1, ChronoUnit.SECONDS), () -> Thread.sleep(2000));
32+
// }
33+
34+
@Test
35+
@DisplayName("测试捕获的异常")
36+
void assertThrowsException() {
37+
String str = null;
38+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
39+
Integer.valueOf(str);
40+
});
41+
}
42+
43+
// @Test
44+
// @DisplayName("测试捕获的异常")
45+
// void assertThrowsExceptionTwo() {
46+
// String str = "A";
47+
// Integer.valueOf(str);
48+
// Assertions.assertThrows(IllegalArgumentException.class, () -> {
49+
// Integer.valueOf(str);
50+
// System.out.println("ABC");
51+
// });
52+
// }
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.coderdream;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
import org.junit.jupiter.params.provider.ValueSource;
7+
8+
/**
9+
* @author :CoderDream
10+
* @date :Created in 2021/9/2 14:22
11+
* @description:基本数据源测试
12+
* @modified By:CoderDream
13+
* @version: 1.0$
14+
*/
15+
public class ParameterizedUnitTest {
16+
@ParameterizedTest
17+
@ValueSource(ints = {2, 4, 8})
18+
void testNumberShouldBeEven(int num) {
19+
Assertions.assertEquals(0, num % 2);
20+
}
21+
22+
@ParameterizedTest
23+
@ValueSource(strings = {"Effective Java", "Code Complete", "Clean Code"})
24+
void testPrintTitle(String title) {
25+
System.out.println(title);
26+
}
27+
28+
@ParameterizedTest
29+
@CsvSource({"1,One", "2,Two", "3,Three"})
30+
void testDataFromCsv(long id, String name) {
31+
System.out.printf("id: %d, name: %s", id, name);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)