File tree Expand file tree Collapse file tree 3 files changed +69
-2
lines changed
main/kotlin/com/memozr/assertk
test/kotlin/com/memozr/assertk Expand file tree Collapse file tree 3 files changed +69
-2
lines changed Original file line number Diff line number Diff line change 1
- # assertk
2
- A kotlin-friendly wrapper for assertJd
1
+ # AssertK - Fluent assertions for Kotlin
2
+ AssertK provides a Kotlin-friendly syntax for using the amazing AssertJ assertion framework.
3
+
4
+ ###Simple assertions
5
+ ```
6
+ assert that Unit isEqualTo Unit
7
+ assert that Any() isNotEqualTo Any()
8
+ assert that Any() _is notNull
9
+ assert that nullObject _is null
10
+ assert that anObject isInstance of<Any>()
11
+ assert that Any() describedAs "A labeled object" isInstance of<Unit>()
12
+ ```
13
+
14
+ ###Chained syntax
15
+ ```
16
+ assert that Unit isNotEqualTo Any() isEqualTo Unit _is notNull isInstance of<Any>()
17
+ ```
18
+
19
+ ###Block syntax
20
+ ```
21
+ assert that Any() isSuchThat {
22
+ it _is notNull
23
+ it isInstance of<Any>()
24
+ it isNotEqualTo Unit
25
+ it isNotEqualTo Any()
26
+ }
27
+ ```
28
+
29
+ ##Assertions on exceptions
30
+ ###Chained syntax
31
+ ```
32
+ assert thatExceptionIsThrownBy { failFunction() } hasMessageContaining "foo" hasCause Throwable()
33
+ ```
34
+
35
+ ###Block syntax
36
+ ```
37
+ assert thatExceptionIsThrownBy {
38
+ throw Throwable("exception foo", Throwable())
39
+ } and {
40
+ it hasMessage "exception foo"
41
+ it hasCause Throwable()
42
+ it hasCauseExactlyInstanceOf Throwable::class.java
43
+ it hasMessageContaining "foo"
44
+ it hasMessageStartingWith "ex"
45
+ it hasMessageEndingWith "foo"
46
+ }
47
+ ```
Original file line number Diff line number Diff line change @@ -47,4 +47,9 @@ class AbstractAssertBuilder<T : Any>(other: T?) {
47
47
assertion.`as `(description)
48
48
return this
49
49
}
50
+
51
+ infix fun isSuchThat (assertionBlock : AbstractAssertBuilder <T >.(AbstractAssertBuilder <T >) -> Unit ): AbstractAssertBuilder <T > {
52
+ assertionBlock(this )
53
+ return this
54
+ }
50
55
}
Original file line number Diff line number Diff line change @@ -59,4 +59,21 @@ class AbstractAssertBuilderTest {
59
59
assert that Any () describedAs " A labeled object" isInstance of<Unit >()
60
60
} hasMessageContaining " A labeled object"
61
61
}
62
+
63
+ @Test
64
+ fun `block assertions` () {
65
+ assert that Any () isSuchThat {
66
+ it _is notNull
67
+ it isInstance of<Any >()
68
+ it isNotEqualTo Unit
69
+ it isNotEqualTo Any ()
70
+ }
71
+
72
+ assert thatExceptionIsThrownBy {
73
+ assert that Any () isSuchThat {
74
+ it _is null
75
+ it isInstance of<Unit >()
76
+ }
77
+ }
78
+ }
62
79
}
You can’t perform that action at this time.
0 commit comments