Skip to content
danseid edited this page Dec 10, 2012 · 15 revisions
should is replaceable by must

Equality

1 should be equal 1 check if 1.equals(1) is true
"1" should !be equal "2" check if !"1".equals("2") is true

Strings

"a" should have length 1 checks if length of “a” is 1
"abc" should start with "a" checks if “abc” is starting with “a”
"abc" should end with "c" checks if “abc” is ending with “c”
"abc" should contain any of("a", "e", "d") checks if “abc” contains “a” or “e” or “d”
"abc" should contain all of("a", "b", "c") checks if “abc” contains “a” and “b” and “c”

Collections

listOf(1, 2, 3) should contain item 3 checks if [1,2,3] contains 3
setOf(1,2,3) should have size 3 checks if [1,2,3] has the size of 3
mapOf(1 to "1", 2 to "2", 3 to "3") should contain item (1 to "1") checks if [(1,“1”),(2,“2”),(3,“3”)] contains (1,“1”)
mapOf(1 to 2, 3 to 4, 5 to 6) should contain key 1 checks if [(1,1),(3,4),(5,6)] contains key 1
mapOf(1 to 2, 3 to 4, 5 to 6) should contain value 2 checks if [(1,1),(3,4),(5,6)] contains vaolue 2

Checking properties and condition of an object

book should match condition {title == "Effective Java" && author == "Joshua Bloch" } checks title and author properties of a book object
1 should match condition {this > 0 && this < 2} checks if 1 > 0 and 1 < 2

Numbers

1 should be gt 0 checks if 1 is greater that 0
1 should be gte 1 checks if 1 is greater that or equal 1
1 should be lt 2 checks if 1 is less that 2
1 should be lte 1 checks if 1 is less that or equal 1

Expecting exception

{ block() } should fail with NullPointerException() checks if block is throwing a NullPointerException
Clone this wiki locally