File tree Expand file tree Collapse file tree
main/java/com/wdbyte/test/junit5
test/java/com/wdbyte/test/junit5 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1313 <artifactId >junit5-jupiter-starter</artifactId >
1414
1515 <properties >
16- <maven .compiler.source>17 </maven .compiler.source>
17- <maven .compiler.target>17 </maven .compiler.target>
16+ <maven .compiler.source>1.8 </maven .compiler.source>
17+ <maven .compiler.target>1.8 </maven .compiler.target>
1818 <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
1919 <maven .compiler.target>${maven.compiler.source} </maven .compiler.target>
2020 </properties >
2121
22- <dependencyManagement >
23- <dependencies >
24- <dependency >
25- <groupId >org.junit</groupId >
26- <artifactId >junit-bom</artifactId >
27- <version >5.8.2</version >
28- <type >pom</type >
29- <scope >import</scope >
30- </dependency >
31- </dependencies >
32- </dependencyManagement >
33-
3422 <dependencies >
3523 <dependency >
3624 <groupId >org.junit.jupiter</groupId >
3725 <artifactId >junit-jupiter</artifactId >
26+ <version >5.9.1</version >
3827 <scope >test</scope >
3928 </dependency >
4029 </dependencies >
4130 <build >
4231 <plugins >
4332 <plugin >
44- <artifactId >maven-compiler -plugin</artifactId >
45- <version >3.8.1 </version >
33+ <artifactId >maven-surefire -plugin</artifactId >
34+ <version >2.22.2 </version >
4635 </plugin >
4736 <plugin >
48- <artifactId >maven-surefire -plugin</artifactId >
37+ <artifactId >maven-failsafe -plugin</artifactId >
4938 <version >2.22.2</version >
5039 </plugin >
5140 </plugins >
5241 </build >
42+
5343</project >
Original file line number Diff line number Diff line change 1+ package com .wdbyte .test .junit5 ;
2+
3+ /**
4+ * @author niulang
5+ * @date 2022/11/17
6+ */
7+ public class Person {
8+
9+ public int getLuckyNumber () {
10+ return 7 ;
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ package com .wdbyte .test .junit5 ;
2+
3+ import org .junit .jupiter .api .AfterAll ;
4+ import org .junit .jupiter .api .AfterEach ;
5+ import org .junit .jupiter .api .Assertions ;
6+ import org .junit .jupiter .api .BeforeAll ;
7+ import org .junit .jupiter .api .BeforeEach ;
8+ import org .junit .jupiter .api .Disabled ;
9+ import org .junit .jupiter .api .DisplayName ;
10+ import org .junit .jupiter .api .Test ;
11+
12+ /**
13+ * @author niulang
14+ * @date 2022/11/15
15+ */
16+ class JUnitBeforeAll {
17+
18+ @ BeforeAll
19+ public static void init () {
20+ System .out .println ("初始化,准备测试信息" );
21+ }
22+
23+ @ BeforeEach
24+ public void start () {
25+ System .out .println ("开始测试..." );
26+ }
27+
28+ @ DisplayName ("是否是狗" )
29+ @ Disabled ("由于xx原因,关闭 testIsDog 测试" )
30+ @ Test
31+ public void testIsDog () {
32+ String name = "dog" ;
33+ Assertions .assertEquals (name , "dog" );
34+ System .out .println ("is dog" );
35+ }
36+
37+ @ DisplayName ("是否是猫" )
38+ @ Test
39+ public void testIsCat () {
40+ String name = "cat" ;
41+ Assertions .assertEquals (name , "cat" );
42+ System .out .println ("is cat" );
43+ }
44+
45+ @ AfterEach
46+ public void end () {
47+ System .out .println ("测试完毕..." );
48+ }
49+
50+ @ AfterAll
51+ public static void close () {
52+ System .out .println ("结束,准备退出测试" );
53+ }
54+ }
Original file line number Diff line number Diff line change 1+ package com .wdbyte .test .junit5 ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .DisplayName ;
5+ import org .junit .jupiter .api .MethodOrderer .OrderAnnotation ;
6+ import org .junit .jupiter .api .Order ;
7+ import org .junit .jupiter .api .Test ;
8+ import org .junit .jupiter .api .TestMethodOrder ;
9+ import org .junit .jupiter .api .condition .EnabledOnJre ;
10+
11+ import static org .junit .jupiter .api .condition .JRE .JAVA_19 ;
12+
13+ /**
14+ * @author niulang
15+ * @date 2022/11/17
16+ */
17+ @ TestMethodOrder (OrderAnnotation .class )
18+ public class JUnitJDKVersion {
19+
20+ @ Test
21+ //@EnabledOnJre(JAVA_19)
22+ @ DisplayName ("测试是否是狗" )
23+ @ Order (2 )
24+ public void testIsDog () {
25+ String name = "dog" ;
26+ Assertions .assertEquals (name , "dog" );
27+ System .out .println ("is dog" );
28+ }
29+
30+ @ DisplayName ("是否是猫" )
31+ @ Test
32+ @ Order (10 )
33+ public void testIsCat () {
34+ String name = "cat" ;
35+ Assertions .assertEquals (name , "cat" );
36+ System .out .println ("is cat" );
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package com .wdbyte .test .junit5 ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .DisplayName ;
5+ import org .junit .jupiter .api .MethodOrderer .OrderAnnotation ;
6+ import org .junit .jupiter .api .Order ;
7+ import org .junit .jupiter .api .Test ;
8+ import org .junit .jupiter .api .TestMethodOrder ;
9+
10+ /**
11+ * @author niulang
12+ * @date 2022/11/17
13+ */
14+ @ TestMethodOrder (OrderAnnotation .class )
15+ public class JUnitOrder {
16+
17+ @ Test
18+ //@EnabledOnJre(JAVA_19)
19+ @ DisplayName ("测试是否是狗" )
20+ @ Order (2 )
21+ public void testIsDog () {
22+ String name = "dog" ;
23+ Assertions .assertEquals (name , "dog" );
24+ System .out .println ("is dog" );
25+ }
26+
27+ @ DisplayName ("是否是猫" )
28+ @ Test
29+ @ Order (1 )
30+ public void testIsCat () {
31+ String name = "cat" ;
32+ Assertions .assertEquals (name , "cat" );
33+ System .out .println ("is cat" );
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package com .wdbyte .test .junit5 ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .DisplayName ;
5+ import org .junit .jupiter .api .MethodOrderer .OrderAnnotation ;
6+ import org .junit .jupiter .api .Order ;
7+ import org .junit .jupiter .api .RepeatedTest ;
8+ import org .junit .jupiter .api .Test ;
9+ import org .junit .jupiter .api .TestMethodOrder ;
10+
11+ /**
12+ * @author niulang
13+ * @date 2022/11/17
14+ */
15+ @ TestMethodOrder (OrderAnnotation .class )
16+ public class JUnitOther {
17+
18+ //@Test
19+ //@EnabledOnJre(JAVA_19)
20+ @ DisplayName ("测试是否是狗" )
21+ @ Order (2 )
22+ @ RepeatedTest (10 )
23+ public void testIsDog () {
24+ String name = "dog" ;
25+ Assertions .assertEquals (name , "dog" );
26+ System .out .println ("is dog" );
27+ }
28+
29+ @ DisplayName ("是否是猫" )
30+ @ Test
31+ @ Order (1 )
32+ public void testIsCat () {
33+ String name = "cat" ;
34+ Assertions .assertEquals (name , "cat" );
35+ System .out .println ("is cat" );
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package com .wdbyte .test .junit5 ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .DisplayName ;
5+ import org .junit .jupiter .params .ParameterizedTest ;
6+ import org .junit .jupiter .params .provider .ValueSource ;
7+
8+ /**
9+ * @author niulang
10+ * @date 2022/11/17
11+ */
12+ public class JUnitParam {
13+
14+ //@Test
15+ @ DisplayName ("是否是狗" )
16+ @ ValueSource (strings = {"dog" , "cat" })
17+ @ ParameterizedTest (name = "开始测试入参 {0} " )
18+ public void testIsDog (String name ) {
19+ Assertions .assertEquals (name , "dog" );
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package com .wdbyte .test .junit5 ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .BeforeAll ;
5+ import org .junit .jupiter .api .Test ;
6+
7+ /**
8+ * @author niulang
9+ * @date 2022/11/15
10+ */
11+ class JUnitTestIsDog {
12+
13+ @ BeforeAll
14+ public static void init () {
15+ System .out .println ("准备测试 Dog 信息" );
16+ }
17+
18+ @ Test
19+ public void testIsDog () {
20+ String name = "cat" ;
21+ Assertions .assertEquals (name , "dog" );
22+ }
23+
24+ @ Test
25+ public void testIsDog2 () {
26+ String name = "dog" ;
27+ Assertions .assertEquals (name , "dog" );
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .wdbyte .test .junit5 ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .DisplayName ;
5+ import org .junit .jupiter .api .Test ;
6+
7+ /**
8+ * @author niulang
9+ * @date 2022/11/17
10+ */
11+ public class JunitAssert {
12+
13+ @ DisplayName ("是否是狗" )
14+ @ Test
15+ public void testIsDog () {
16+ String name = "dog" ;
17+ Assertions .assertNotNull (name );
18+ Assertions .assertEquals (name , "dog" );
19+ Assertions .assertNotEquals (name , "cat" );
20+ Assertions .assertTrue ("dog" .equals (name ));
21+ Assertions .assertFalse ("cat" .equals (name ));
22+ }
23+
24+ @ DisplayName ("是否是猫" )
25+ @ Test
26+ public void testIsCat () {
27+ String name = "cat" ;
28+ Assertions .assertNull (name , "name is not null" );
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ package com .wdbyte .test .junit5 ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .DisplayName ;
5+ import org .junit .jupiter .api .Test ;
6+
7+ /**
8+ * @author niulang
9+ * @date 2022/11/17
10+ */
11+ @ DisplayName ("测试 Presion" )
12+ class PersonTest {
13+
14+ @ DisplayName ("测试幸运数字" )
15+ @ Test
16+ void getLuckyNumber () {
17+ Person person = new Person ();
18+ Assertions .assertEquals (8 , person .getLuckyNumber ());
19+ }
20+ }
You can’t perform that action at this time.
0 commit comments