简体   繁体   English

委派org.junit.jupiter.api.Assertions类

[英]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 然而

  1. org.junit.jupiter.api.Assertions is a long class to delegate. org.junit.jupiter.api.Assertions是一个很长的委托类。

  2. 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.

相关问题 Junit 测试错误:org/junit/jupiter/api/extension/ScriptEvaluationException - Junit Test error : org/junit/jupiter/api/extension/ScriptEvaluationException Eclipse Java IDE JUnit5:junit.jupiter.api.Assertions 不可访问 - Eclipse Java IDE JUnit5: junit.jupiter.api.Assertions is not accessible java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/ScriptEvaluationException - java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/ScriptEvaluationException org.junit.jupiter.api.extension.ParameterResolutionException:没有为参数注册ParameterResolver - org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter JUnit java.lang.NoSuchMethodError: org.junit.jupiter.api.extension.ExtensionContext.getRequiredTestInstances() - JUnit java.lang.NoSuchMethodError: org.junit.jupiter.api.extension.ExtensionContext.getRequiredTestInstances() JUnit 5 migration - how to solve java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.ReflectiveInvocationContext - JUnit 5 migration - how to solve java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.ReflectiveInvocationContext junit-jupiter-api 和 junit-jupiter-engine 的区别 - Difference between junit-jupiter-api and junit-jupiter-engine 如何修复“ import org.junit.jupiter”? - how to fix “The import org.junit.jupiter”? 导入org.junit.jupiter 无法解决 - The import org.junit.jupiter cannot be resolved Maven Surefire 插件未从 org.junit.jupiter.api 包记录 - Maven Surefire Plugin not logging from org.junit.jupiter.api package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM