1
+ package com.baeldung.atrium
2
+
3
+ import ch.tutteli.atrium.api.fluent.en_GB.*
4
+ import ch.tutteli.atrium.api.infix.en_GB.because
5
+ import ch.tutteli.atrium.api.infix.en_GB.toBeAnInstanceOf
6
+ import ch.tutteli.atrium.api.infix.en_GB.toBeTheInstance
7
+ import ch.tutteli.atrium.api.infix.en_GB.toMatch
8
+ import ch.tutteli.atrium.api.verbs.expect
9
+ import ch.tutteli.atrium.api.verbs.expectGrouped
10
+ import ch.tutteli.atrium.creating.Expect
11
+ import ch.tutteli.atrium.logic._logic
12
+ import ch.tutteli.atrium.reporting.AtriumError
13
+ import org.junit.jupiter.api.Disabled
14
+ import org.junit.jupiter.api.Test
15
+
16
+ class FluentAssertionUnitTest {
17
+ @Test
18
+ fun equalsSuccess () {
19
+ val value = " Hello"
20
+
21
+ expect(value).toEqual(" Hello" )
22
+ }
23
+
24
+ @Test
25
+ @Disabled
26
+ fun equalsFailure () {
27
+ val value = " Hello"
28
+
29
+ expect(value).toEqual(" Hello, World!" )
30
+ }
31
+
32
+ @Test
33
+ @Disabled
34
+ fun chainedFailure () {
35
+ val value = - 3
36
+ expect(value).toBeGreaterThan(0 ).toBeLessThan(5 )
37
+ }
38
+
39
+ @Test
40
+ @Disabled
41
+ fun multipleFailures () {
42
+ val value = " Wrong"
43
+ expect(value) {
44
+ toEqual(" Wrong" )
45
+ toStartWith(" Hello" )
46
+ toEndWith(" World" )
47
+ }
48
+ }
49
+
50
+ @Test
51
+ @Disabled
52
+ fun equalsFailureWithReason () {
53
+ val value = " Hello"
54
+
55
+ expect(value).because(" that's the value we expected" ) {
56
+ toEqual(" Hello, World!" )
57
+ }
58
+ }
59
+
60
+ @Test
61
+ @Disabled
62
+ fun chainedPropertyExtraction () {
63
+ data class User (val username : String , val displayName : String ) {
64
+ fun isEnabled () = true
65
+ }
66
+
67
+ val user = User (" baeldung" , " Baeldung" )
68
+
69
+ expect(user)
70
+ .its(User ::username) { toEqual(" Username" ) }
71
+ .its(User ::displayName) { toEqual(" Display Name" ) }
72
+ .its(User ::isEnabled) { toEqual(true ) }
73
+ }
74
+
75
+ @Test
76
+ @Disabled
77
+ fun multiplePropertyExtraction () {
78
+ data class User (val username : String , val displayName : String ) {
79
+ fun isEnabled () = true
80
+
81
+ }
82
+
83
+ val user = User (" baeldung" , " Baeldung" )
84
+
85
+ expect(user) {
86
+ its(User ::username) { toEqual(" Username" ) }
87
+ its(User ::displayName) { toEqual(" Display Name" ) }
88
+ its(User ::isEnabled) { toEqual(true ) }
89
+ }
90
+ }
91
+
92
+ @Test
93
+ @Disabled
94
+ fun multipleFeatureExtraction () {
95
+ data class User (val username : String , val displayName : String ) {
96
+ fun isEnabled () = true
97
+ }
98
+
99
+
100
+
101
+ val user = User (" baeldung" , " Baeldung" )
102
+
103
+ expect(user) {
104
+ feature({f(it::username)}) { toEqual(" Username" ) }
105
+ feature({f(it::displayName)}) { toEqual(" Display Name" ) }
106
+ feature({f(it::isEnabled)}) { toEqual(true ) }
107
+ }
108
+ }
109
+
110
+ @Test
111
+ @Disabled
112
+ fun typeAssertions () {
113
+ open class Animal (val name : String )
114
+ class Dog (name : String , val breed : String ) : Animal(name)
115
+ class Cat (name : String , val color : String ) : Animal(name)
116
+
117
+ val animal = Dog (" Lassie" , " Rough collie" )
118
+
119
+ expect(animal).toBeAnInstanceOf<Dog > {
120
+ its(Animal ::name).toEqual(" Lassie" )
121
+ its(Dog ::breed).toEqual(" Pomeranian" )
122
+ }
123
+ }
124
+
125
+ @Test
126
+ @Disabled
127
+ fun groupedAssertions () {
128
+ expectGrouped {
129
+ group(" First group" ) {
130
+ expect(" Hello" ).toEqual(" World" )
131
+ expect(" Hello" ).toEqual(" Again" )
132
+ }
133
+ group(" Second group" ) {
134
+ expect(" Hello" ).toEqual(" World" )
135
+ }
136
+ }
137
+ }
138
+
139
+ @Test
140
+ @Disabled
141
+ fun dataDrivenTest () {
142
+ expectGrouped {
143
+ for (i in 1 .. 3 ) {
144
+ group(" Group ${i} " ) {
145
+ expect(i).toBeLessThan(2 )
146
+ }
147
+ }
148
+ }
149
+ }
150
+
151
+ @Test
152
+ @Disabled
153
+ fun customAssertion () {
154
+ expectGrouped {
155
+ group(" toBeMultipleOf" ) {
156
+ expect(3 ).toBeAMultipleOf(2 )
157
+ }
158
+
159
+ group(" toBeBetween" ) {
160
+ expect(3 ).toBeBetween(5 , 10 )
161
+ expect(3 ).toBeBetween(0 , 2 )
162
+ }
163
+
164
+ group(" User" ) {
165
+ data class User (val username : String , val displayName : String ) {
166
+ fun isEnabled () = true
167
+ }
168
+
169
+ fun Expect<User>.toHaveUsername (username : String ) = its(User ::username) { toEqual(username) }
170
+
171
+ val user = User (" baeldung" , " Baeldung" )
172
+ expect(user).toHaveUsername(" Other" )
173
+ }
174
+ }
175
+ }
176
+ }
177
+
178
+ fun Expect<Int>.toBeAMultipleOf (base : Int ) =
179
+ _logic .createAndAppend(" to be a multiple of" , base) { it % base == 0 }
180
+
181
+ fun Expect<Int>.toBeBetween (low : Int , high : Int ) = and {
182
+ toBeGreaterThan(low)
183
+ toBeLessThan(high)
184
+ }
0 commit comments