Grails 2.3.4
Given the following domain class:
class Player {
String name
static constraints = {
name nullable: false, size: 2..30, unique: true
}
}
Running the following unit tests produce some strange behavior.
Testing using shouldFail()
@Test
void nameUniqueContraint() {
Player player = new Player(name: "John")
Player player2 = new Player(name: player.name)
assert(player.save())
shouldFail(ValidationException) {
player2.save(failOnError: true, flush: true)
fail "FAIL ME"
}
}
Test result:
grails.validation.ValidationException: Validation error occurred during call to save():
- Field error in object 'moonillusions.sulis.domain.Player' on field 'name': rejected value [John]
Testing using shouldFail() and catch
@Test
void nameUniqueContraint1() {
Player player = new Player(name: "John")
Player player2 = new Player(name: player.name)
assert(player.save())
shouldFail(ValidationException) {
try {
player2.save(failOnError: true, flush: true)
fail "FAIL ME"
}catch(ValidationException e) {
fail "CATCHED "
}
}
}
Test result:
junit.framework.AssertionFailedError: CATCHED
Testing using catch
@Test
void nameUniqueContraint2() {
Player player = new Player(name: "John")
Player player2 = new Player(name: player.name)
assert(player.save())
try {
player2.save(failOnError: true, flush: true)
fail "FAIL ME"
}catch(ValidationException e) {
fail "CATCHED "
}
}
Test result:
junit.framework.AssertionFailedError: FAIL ME
For me these results do not make any sense. shouldFail() seems not to behave us expected (test 1) and also affecting the try block (test 2) inside the code.