Skip to content

Commit 7613001

Browse files
committed
example: junit5
1 parent 67537d8 commit 7613001

13 files changed

Lines changed: 603 additions & 0 deletions

File tree

codes/javalib/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
<module>bean</module>
1515
<module>log</module>
1616
<module>mvel</module>
17+
<module>test</module>
1718
</modules>
1819
</project>

codes/javalib/test/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
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>io.github.dunwu.javalib</groupId>
8+
<artifactId>javalib-test</artifactId>
9+
<version>1.0.1</version>
10+
<packaging>jar</packaging>
11+
<name>JavaLib Demos - TEST</name>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<java.version>1.8</java.version>
16+
<maven.compiler.source>${java.version}</maven.compiler.source>
17+
<maven.compiler.target>${java.version}</maven.compiler.target>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>ch.qos.logback</groupId>
23+
<artifactId>logback-classic</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-api</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter-params</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-engine</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.mockito</groupId>
42+
<artifactId>mockito-core</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
<dependencyManagement>
47+
<dependencies>
48+
<dependency>
49+
<groupId>io.github.dunwu</groupId>
50+
<artifactId>dunwu-bom</artifactId>
51+
<version>0.0.3</version>
52+
<type>pom</type>
53+
<scope>import</scope>
54+
</dependency>
55+
</dependencies>
56+
</dependencyManagement>
57+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.dunwu.javalib.bean;
2+
3+
public class Calculator {
4+
5+
public int add(int a, int b) {
6+
return a + b;
7+
}
8+
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.github.dunwu.javalib.bean;
2+
3+
/**
4+
* @author Zhang Peng
5+
* @date 2018-11-29
6+
*/
7+
public class Person {
8+
private String firstName;
9+
private String lastName;
10+
11+
public Person(String firstName, String lastName) {
12+
this.firstName = firstName;
13+
this.lastName = lastName;
14+
}
15+
16+
public String getFirstName() {
17+
return firstName;
18+
}
19+
20+
public void setFirstName(String firstName) {
21+
this.firstName = firstName;
22+
}
23+
24+
public String getLastName() {
25+
return lastName;
26+
}
27+
28+
public void setLastName(String lastName) {
29+
this.lastName = lastName;
30+
}
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] [%p] %c#%M - %m%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<logger name="io.github.dunwu" level="INFO"/>
10+
11+
<root level="INFO">
12+
<appender-ref ref="CONSOLE"/>
13+
</root>
14+
</configuration>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package io.github.dunwu.javalib.junit5;
2+
3+
import io.github.dunwu.javalib.bean.Person;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static java.time.Duration.ofMillis;
8+
import static java.time.Duration.ofMinutes;
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
/**
12+
* Junit5 断言示例
13+
* @author Zhang Peng
14+
* @date 2018-11-29
15+
*/
16+
class AssertionsTests {
17+
18+
private static Person person;
19+
20+
@BeforeAll
21+
public static void beforeAll() {
22+
person = new Person("John", "Doe");
23+
}
24+
25+
@Test
26+
void standardAssertions() {
27+
assertEquals(2, 2);
28+
assertEquals(4, 4, "The optional assertion message is now the last parameter.");
29+
assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- "
30+
+ "to avoid constructing complex messages unnecessarily.");
31+
}
32+
33+
@Test
34+
void groupedAssertions() {
35+
// In a grouped assertion all assertions are executed, and any
36+
// failures will be reported together.
37+
assertAll("person", () -> assertEquals("John", person.getFirstName()),
38+
() -> assertEquals("Doe", person.getLastName()));
39+
}
40+
41+
@Test
42+
void dependentAssertions() {
43+
// Within a code block, if an assertion fails the
44+
// subsequent code in the same block will be skipped.
45+
assertAll("properties", () -> {
46+
String firstName = person.getFirstName();
47+
assertNotNull(firstName);
48+
49+
// Executed only if the previous assertion is valid.
50+
assertAll("first name", () -> assertTrue(firstName.startsWith("J")),
51+
() -> assertTrue(firstName.endsWith("n")));
52+
}, () -> {
53+
// Grouped assertion, so processed independently
54+
// of results of first name assertions.
55+
String lastName = person.getLastName();
56+
assertNotNull(lastName);
57+
58+
// Executed only if the previous assertion is valid.
59+
assertAll("last name", () -> assertTrue(lastName.startsWith("D")),
60+
() -> assertTrue(lastName.endsWith("e")));
61+
});
62+
}
63+
64+
@Test
65+
void exceptionTesting() {
66+
Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
67+
throw new IllegalArgumentException("a message");
68+
});
69+
assertEquals("a message", exception.getMessage());
70+
}
71+
72+
@Test
73+
void timeoutNotExceeded() {
74+
// The following assertion succeeds.
75+
assertTimeout(ofMinutes(2), () -> {
76+
// Perform task that takes less than 2 minutes.
77+
});
78+
}
79+
80+
@Test
81+
void timeoutNotExceededWithResult() {
82+
// The following assertion succeeds, and returns the supplied object.
83+
String actualResult = assertTimeout(ofMinutes(2), () -> {
84+
return "a result";
85+
});
86+
assertEquals("a result", actualResult);
87+
}
88+
89+
@Test
90+
void timeoutNotExceededWithMethod() {
91+
// The following assertion invokes a method reference and returns an object.
92+
String actualGreeting = assertTimeout(ofMinutes(2), AssertionsTests::greeting);
93+
assertEquals("Hello, World!", actualGreeting);
94+
}
95+
96+
@Test
97+
void timeoutExceeded() {
98+
// The following assertion fails with an error message similar to:
99+
// execution exceeded timeout of 10 ms by 91 ms
100+
assertTimeout(ofMillis(10), () -> {
101+
// Simulate task that takes more than 10 ms.
102+
Thread.sleep(100);
103+
});
104+
}
105+
106+
@Test
107+
void timeoutExceededWithPreemptiveTermination() {
108+
// The following assertion fails with an error message similar to:
109+
// execution timed out after 10 ms
110+
assertTimeoutPreemptively(ofMillis(10), () -> {
111+
// Simulate task that takes more than 10 ms.
112+
Thread.sleep(100);
113+
});
114+
}
115+
116+
private static String greeting() {
117+
return "Hello, World!";
118+
}
119+
120+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.dunwu.javalib.junit5;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
7+
import static org.junit.jupiter.api.Assumptions.assumingThat;
8+
9+
/**
10+
* Junit5 断言示例
11+
* @author Zhang Peng
12+
* @date 2018-11-29
13+
*/
14+
class AssumptionsTests {
15+
16+
@Test
17+
void testOnlyOnCiServer() {
18+
assumeTrue("CI".equals(System.getenv("ENV")));
19+
// remainder of test
20+
}
21+
22+
@Test
23+
void testOnlyOnDeveloperWorkstation() {
24+
assumeTrue("DEV".equals(System.getenv("ENV")), () -> "Aborting test: not on developer workstation");
25+
// remainder of test
26+
}
27+
28+
@Test
29+
void testInAllEnvironments() {
30+
assumingThat("CI".equals(System.getenv("ENV")), () -> {
31+
// perform these assertions only on the CI server
32+
assertEquals(2, 2);
33+
});
34+
35+
// perform these assertions in all environments
36+
assertEquals("a string", "a string");
37+
}
38+
39+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.dunwu.javalib.junit5;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* Junit5 定制测试类和方法的显示名称
8+
* @author Zhang Peng
9+
* @date 2018-11-29
10+
*/
11+
@DisplayName("A special test case")
12+
class DisplayNameTests {
13+
14+
@Test
15+
@DisplayName("Custom test name containing spaces")
16+
void testWithDisplayNameContainingSpaces() { }
17+
18+
@Test
19+
@DisplayName("╯°□°)╯")
20+
void testWithDisplayNameContainingSpecialCharacters() { }
21+
22+
@Test
23+
@DisplayName("😱")
24+
void testWithDisplayNameContainingEmoji() { }
25+
}

0 commit comments

Comments
 (0)