首页 > 解决方案 > 如何在 JUnit 中显式或手动失败测试用例

问题描述

在我的 Java / RestAssured / TestNG 框架中,我想编写一个具有以下结构的断言

if(responseString.equals("Good Response!")) {
  // perform additional assertions here
} else if(responseString.equals("Bad Response!")) {
  // just fail the test case right here and now
}

在不断言任何其他内容的情况下简单地使测试用例失败的最佳方法是什么?

标签: javatestingjunitrest-assured

解决方案


不要使用 if,而只是这样做:

assertEquals("Good Response!", responseString);
// perform additional assertions here

如果你只是想失败:

fail("I failed because...");

推荐阅读