如何编写测试来检查无效配置会导致Spring应用程序上下文失败?

时间:2013-12-04 17:22:04

标签: java spring testing

我们的团队使用Spring TestContext framework为Spring编写测试。

它非常易于使用,但我找不到测试无效上下文配置的方法。

我尝试使用无效配置将@Test(expected = Exception.class)置于测试中,并且java.lang.IllegalStateException: Failed to load ApplicationContext无论如何测试失败。

1 个答案:

答案 0 :(得分:0)

为了测试失败,我通常在阴性测试中执行以下操作:

try{
    //code that should cause exception
    fail(); //or equivalent for your test framework
}catch(Exception e){ //your exception type can be more specific if you want
    // code that examines exception and confirms it's expected (if any)
}