【问题标题】:Surefire marks tests as failed when they set SecurityManagerSurefire 在设置 SecurityManager 时将测试标记为失败
【发布时间】:2022-07-12 16:42:46
【问题描述】:

我有一些测试需要检查主代码是否执行了 System.exit(...)。这与来自https://stackoverflow.com/a/309427/1023341 的建议非常有效。但是当在 Jenkins 中运行这些测试时(而不是在我的 IDE Eclipse 中)以及稍后在使用 Maven-Surefire 的命令行上尝试它们时(就像 Jenkins 所做的那样),测试失败而没有告诉我原因。它只告诉我:Error occurred in starting fork, check output in log

【问题讨论】:

    标签: maven maven-surefire-plugin securitymanager java-security-manager


    【解决方案1】:

    在 JUnit (5) 期间使用 System.setSecurityManager 和 Surefire 插件设置 SecurityManager 时,应在测试后恢复 SecurityManager。

    SecurityManager origSecurityManager = System.getSecurityManager();
    try {
         // ... code under test here ...
    } finally {
         System.setSecurityManager(origSecurityManager);
    }
    

    或其他更合适的形式。这可以确保 Maven-Surefire-plugin 保持愉快。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多