我正在编写一个测试自动化框架,并试图尽可能简化我的用户的生活。我希望我的用户只将断言作为常规的Junit 5测试,日志写入(我的Log4J实例)、报告条目(扩展报告)都将在断言中完成。
因此,我想委托org.junit.jupiter.api.Assertions类,以便:
assertTrue(myCondition, "My Message");将执行以下操作(我复制了原始assertTrue并添加了我的功能):
package org.junit.jupiter.api;
@API(status = STABLE, since = "5.0")
public class Assertions {
//...... Some original org.junit.jupiter.api.Assertions functions
public static void assertTrue(boolean condition, String message) {
try{
AssertTrue.assertTrue(condition, message);
}
catch(AssertionError error){
//Do my things - reporter and logger
throw error;
}
}
//...... Some original org.junit.jupiter.api.Assertions functions
}然而,
org.junit.jupiter.api.Assertions是一个需要委托的长类。AssertTrue只在包级别上可见。想要对如何优雅地解决这个问题有一些新的想法.谢谢,
发布于 2018-11-01 13:22:33
好的,
最后,我创建了一个新的DelegatingAssert类,对于我感兴趣的每一个断言,我创建了以下内容:
public static void assertFalse(DelegatingExtentTest testCase, boolean condition, String message) {
try{
Assertions.assertFalse(condition);
testCase.pass(message);
}
catch(AssertionError e) {
testCase.fail("Did not: " + message);
getLogger().error("Fail message: " + e.getMessage());
getLogger().error("Fail stack trace: " + Helper.getStackTrace(e));
throw e;
}
}https://stackoverflow.com/questions/53082450
复制相似问题