-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathAssertTest.java
110 lines (88 loc) · 2.83 KB
/
AssertTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package org.codefx.demo.junit5.basics;
import org.junit.jupiter.api.Test;
import java.util.LinkedList;
import java.util.List;
import static java.time.Duration.of;
import static java.time.temporal.ChronoUnit.MILLIS;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
class AssertTest {
@Test
void assertWithBoolean() {
assertTrue(true);
assertTrue(this::truism);
assertFalse(false, () -> "Really " + "expensive " + "message" + ".");
}
private boolean truism() {
return true;
}
@Test
void assertWithComparison() {
List<String> expected = asList("element");
List<String> actual = new LinkedList<>(expected);
assertEquals(expected, actual);
assertEquals(expected, actual, "Different 'List' implementations should be equal.");
assertEquals(expected, actual, () -> "Different " + "'List' implementations " + "should be equal.");
assertNotSame(expected, actual, "Obviously not the same instance.");
}
@Test
void failTheTest_fails() {
fail("epicly");
}
@Test
void assertAllProperties_fails() {
Address address = new Address("New City", "Some Street", "No");
assertAll("address",
() -> assertEquals("Neustadt", address.city),
() -> assertEquals("Irgendeinestraße", address.street),
() -> assertEquals("Nr", address.number)
);
}
static class Address {
final String city;
final String street;
final String number;
private Address(String city, String street, String number) {
this.city = city;
this.street = street;
this.number = number;
}
}
@Test
void assertExceptions() {
Exception exception = assertThrows(Exception.class, this::throwing);
assertEquals("I'm failing on purpose.", exception.getMessage());
}
private void throwing() {
throw new IndexOutOfBoundsException("I'm failing on purpose.");
}
@Test
void assertTimeout_runsLate_failsButFinishes() {
assertTimeout(of(100, MILLIS), () -> {
sleepUninterrupted(250);
// you will see this message
System.out.println("Woke up");
});
}
@Test
void assertTimeoutPreemptively_runsLate_failsAndAborted() {
assertTimeoutPreemptively(of(100, MILLIS), () -> {
sleepUninterrupted(250);
// you will NOT see this message
System.out.println("Woke up");
});
}
private static void sleepUninterrupted(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ex) { }
}
}