-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoryTest.java
More file actions
104 lines (90 loc) · 3.3 KB
/
StoryTest.java
File metadata and controls
104 lines (90 loc) · 3.3 KB
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
package com.example.testing.assertj;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
class StoryTest {
@Nested
class WithoutAssertJ {
@Test
@Tag("failing")
void one_by_one_failure() {
var story = Story.builder()
.name("Test Story")
.description("Test Description")
.build();
assertEquals("Test story", story.getName());
assertEquals("Test description", story.getDescription());
}
}
@Nested
class WithAssertJ {
/**
* SoftAssertions allows you to get feedback on several assertions rather than
* just the first one to fail.
*/
@Test
@Tag("failing")
void softly_assert() {
var story = Story.builder()
.name("Test Story")
.description("Test Description")
.build();
var softly = new SoftAssertions();
softly.assertThat(story.getName()).isEqualTo("Test story");
softly.assertThat(story.getDescription()).isEqualTo("Test description");
softly.assertAll();
}
/**
* AssertJ asserions can be chained, making your tests more readable.
*/
@Test
void chained_asserts() {
var story = Story.builder()
.name("Test")
.acceptanceCriteria(List.of(
"It should do the good thing",
"It should not do the bad thing"
))
.build();
assertThat(story.getAcceptanceCriteria())
.hasSize(2)
.contains("It should do the good thing",
"It should not do the bad thing");
}
@Test
void custom_assertion() {
var story = Story.builder()
.name("ABC-12345: Implement Feature")
.size(Story.Size.COLOSSAL)
.build();
// You can mix and match custom assertions with built-ins.
StoryAssert.assertThat(story)
.matches(s -> s.getSize().equals(Story.Size.COLOSSAL))
.hasJiraTicket();
}
/**
* Custom assertions can be created to give names to common assertions,
* making your tests more coherent.
*/
static class StoryAssert extends AbstractAssert<StoryAssert, Story> {
protected StoryAssert(Story actual) {
super(actual, StoryAssert.class);
}
public static StoryAssert assertThat(Story actual) {
return new StoryAssert(actual);
}
public StoryAssert hasJiraTicket() {
isNotNull();
if (!actual.getName().matches("[A-Z]+-\\d+[\s:]?\\s.+?")) {
failWithMessage("Expected story to contain ticket number");
}
return this;
}
}
}
}