如何解決Springboot測試時的動態加載Agent警告問題?

如何解決Springboot測試時的動態加載Agent警告問題?

spring Boot單元測試:消除動態加載Agent警告

在進行spring boot單元測試時,你可能會遇到惱人的動態加載Agent警告:

warning: a Java agent has been loaded dynamically warning: if a serviceability tool is in use, please run with -xx:+enabledynamicagentloading to hide this warning warning: if a serviceability tool is not in use, please run with -djdk.instrument.traceusage for more information warning: dynamic loading of agents will be disallowed by default in a future release openjdk 64-bit server vm warning: sharing is only supported for boot loader classes because bootstrap classpath has been appended

此警告表明你的Java代理(Agent)被動態加載,未來版本將默認禁止此行為。 你可能嘗試過取消idea的代理檢測或添加-xshare:off和-xx:+enabledynamicagentloading參數,但效果不佳。

以下是一些更有效的解決方案:

  1. 正確配置-xx:+enabledynamicagentloading參數: 確保在正確的構建配置文件中添加此參數。例如,在gradle中:

     test {      jvmArgs('-xx:+enabledynamicagentloading')  }

    或在maven的surefire-plugin配置中:

     <plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-surefire-plugin</artifactId>      <configuration>          <argLine>-xx:+enabledynamicagentloading</argLine>      </configuration>  </plugin>
  2. 使用-Djdk.instrument.traceUsage參數: 若確定未使用服務性工具,此參數可提供更多診斷信息:

     test {      jvmArgs('-Djdk.instrument.traceUsage')  }
  3. 檢查IDEA運行配置: 確保IDEA運行配置中未設置任何與代理相關的沖突配置。

  4. 升級JDK版本: 警告有時源于JDK版本問題,嘗試升級至最新版本。

  5. 排查依賴和插件: 仔細檢查項目依賴和插件,找出可能動態加載Java代理的組件,考慮禁用或移除。

通過以上方法,你應該能夠有效地解決Spring Boot單元測試中的動態加載Agent警告。

? 版權聲明
THE END
喜歡就支持一下吧
點贊8 分享