An example of JUnit extensions in Kotlin, allowing:
- Use good-old JUnit (runner, outputs) - e.g. unlike Spek
- Easy-to-read / Fun-to-write Syntax (inspired by Jasmine)
- expose Kotlin std lib capabilities - e.g. regex matching, deep comparison of nested arrays
- Kotlin native, allowing expressions such as
expect { 1 to "2" toBe Pair(1,"2") }
- I tried to improve JUnit erorr messages for trackability
**
expect(<extra details>) { ... }
format - to expose extra details in the error message in times of failure
import org.junit.Test
import com.baronlior.junit4.Assert
class SyntaxDocumentation {
@Test
fun myTestMethod() {
it("The 'toBe' matcher compares with .equals()") {
expect { 2 + 2 toBe 4 }
expect { null toBe null }
expect { 1 to "2" toBe Pair(1,"2") }
expect ("string concatenation") { "2" + "2" notToBe "4" }
expect { "null" notToBe null }
}
it("The 'toBeLessThan' matcher is for mathematical comparisons") {
val PI = 3.1415926
val e = 2.78
expect {e toBeLessThan PI}
}
it("The 'toMatch' matcher is for regular expressions") {
val message = "foo bar baz";
expect { message toMatchRegex "bar" }
expect { message notToMatchRegex "quux" }
}
}
}
Right now: just copy Assert.kt
and Utility.kt
to your project