groovy

GroovyTestCase.shouldFail{} should return the message of the enclosed Exception

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0-JSR-5
  • Fix Version/s: 1.0-JSR-6
  • Component/s: None
  • Labels:
    None
  • Testcase included:
    yes
  • Number of attachments :
    0

Description

GroovyTestCase.shouldFail{} should return the message of the enclosed Exception.
This would allow easier testing that the Exception reveals a meaningful message.

def result = shouldFail(IllegalArgumentException.class){
    throw new IllegalArgumentException('null not allowed')
}
assertEquals 'null not allowed', result

Activity

Hide
Olivier Gourment added a comment -

Thanks Dierk for this useful feature. Here is another example:

assert (shouldFail { this.methodThatDoesNotExist(404) }).contains("MissingMethodException")

Show
Olivier Gourment added a comment - Thanks Dierk for this useful feature. Here is another example: assert (shouldFail { this.methodThatDoesNotExist(404) }).contains("MissingMethodException")
Hide
Olivier Gourment added a comment -

Ok, actually, there's more to it.

class VarargsTests extends GroovyTestCase {
static int add(int a, int b) { a + b }
void testVarargs() {
shouldFail (MissingMethodException.class) { add(1,"2") }
assert shouldFail ({ add(1,"2") }).contains("MissingMethodException")
shouldFailWithCause (MissingMethodException.class, { add(1,"2") })
}
}

Show
Olivier Gourment added a comment - Ok, actually, there's more to it. class VarargsTests extends GroovyTestCase { static int add(int a, int b) { a + b } void testVarargs() { shouldFail (MissingMethodException.class) { add(1,"2") } assert shouldFail ({ add(1,"2") }).contains("MissingMethodException") shouldFailWithCause (MissingMethodException.class, { add(1,"2") }) } }

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: