You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You might be asking, "If AssertJ already exists, why create another library?". It's true, assertk is very similar to AssertJ. But assertk is written in Kotlin so it has one major advantage: extension methods. This makes adding your own assertion methods far simpler.
14
+
15
+
See [Custom Assertions](#custom-assertions) below to find out how to do this.
7
16
8
17
## Setup
9
18
@@ -64,13 +73,15 @@ Since null is a first-class concept in kotlin's type system, you need to be expl
64
73
val nullString:String?=null
65
74
assertThat(nullString).hasLength(4)
66
75
```
76
+
67
77
will not compile, since `hasLength()` only makes sense on non-null values. You can chain `isNotNull()` to handle this.
68
78
69
79
```kotlin
70
80
val nullString:String?=null
71
81
assertThat(nullString).isNotNull().hasLength(4)
72
82
// -> expected to not be null
73
83
```
84
+
74
85
This will first ensure the string is not null before running any other checks.
75
86
76
87
### Multiple assertions
@@ -102,6 +113,7 @@ assertAll {
102
113
```
103
114
104
115
### Iterable/List Assertions
116
+
105
117
You can assert on the contents of an `Iterable/List` with the various `contains*` functions. They have different
106
118
semantics as follows:
107
119
@@ -116,7 +128,7 @@ semantics as follows:
116
128
117
129
### Extracting data
118
130
119
-
There's a few ways you extract the data you want to assert on. While you can do this yourself before calling the
131
+
There's a few ways you extract the data you want to assert on. While you can do this yourself before calling the
120
132
assertion, these methods will add the extra context to the failure message which can be helpful.
121
133
122
134
The simplest way is with `prop()`. It will take a property (or function, or a name and a lambda) and return an
@@ -163,6 +175,7 @@ assertThat {
163
175
```
164
176
165
177
This method also allows you to assert on successfully returned values.
178
+
166
179
```kotlin
167
180
assertThat { 1+1 }.isSuccess().isNegative()
168
181
// -> expected to be negative but was:<2>
@@ -241,4 +254,5 @@ The general rule of thumb is to prefer building out of the existing assertions u
241
254
error message.
242
255
243
256
## Contributing to assertk
244
-
Contributions are more than welcome! Please see the [Contributing Guidelines](https://github.com/willowtreeapps/assertk/blob/master/Contributing.md) and be mindful of our [Code of Conduct](https://github.com/willowtreeapps/assertk/blob/master/code-of-conduct.md).
257
+
258
+
Contributions are more than welcome! Please see the [Contributing Guidelines](https://github.com/willowtreeapps/assertk/blob/main/Contributing.md) and be mindful of our [Code of Conduct](https://github.com/willowtreeapps/assertk/blob/main/code-of-conduct.md).
0 commit comments