简洁'断言封闭抛出'Groovy单元测试的表达式?

时间:2013-12-19 14:27:50

标签: java unit-testing groovy closures assertions

我需要类似的东西:

assertThat({throw new Exception("hey!")}).throws(Exception).hasMessage("hey!");

在Java中我使用了标准方法,但对于带闭包的语言来说太过分了:

try{
    throw new Exception("hey!");
    fail("not thrown");
}catch (Exception e){
    assertThat(e).hasMessage("hey!");
}

非常欢迎使用JUnit,Hamcrest,Fest或Mockito等框架。

1 个答案:

答案 0 :(得分:1)

我在@OliCharlesworth的帮助下找到的最接近的匹配是来自FestAssert库的assertThat和来自GroovyAssertshouldFail

assertThat(shouldFail(Exception, {throw new Exception("hey")})).hasMessage("hey")