[英]Delegating org.junit.jupiter.api.Assertions class
I am writing a test automation framework, and trying to simplify life for my users as much as possible. 我正在编写一个测试自动化框架,并试图尽可能简化用户的生活。 I would like my users to just assert as regular Junit 5 test, and the log writing (my instance of Log4J), report entry (Extent Report) will all be done within the assert. 我希望我的用户仅断言为常规的Junit 5测试,并且日志编写(我的Log4J实例),报告条目(范围报告)都将在断言中完成。
So, I would like to delegate org.junit.jupiter.api.Assertions class so that: 因此,我想委托org.junit.jupiter.api.Assertions类,以便:
assertTrue(myCondition, "My Message");
Will do the following (I copied the original assertTrue
and added my functionality): 将执行以下操作(我复制了原始的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
}
However 然而
org.junit.jupiter.api.Assertions
is a long class to delegate. org.junit.jupiter.api.Assertions
是一个很长的委托类。
it becomes complicated since AssertTrue
is only visible in package level. 由于AssertTrue
仅在包级别可见,因此变得很复杂。
Would like to get some fresh thoughts on how to resolve it elegantly.... Thanks, 希望对如何优雅地解决它有一些新的想法。...谢谢,
OK, 好,
What I ended up doing was creating a new DelegatingAssert
class, and for every Assert I was interested, I created the following: 我最终要做的是创建一个新的DelegatingAssert
类,对于我感兴趣的每个Assert,我都创建了以下内容:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.