【问题标题】:Add vm options -Djava.util.Arrays.useLegacyMergeSort=true in pom.xml在 pom.xml 中添加 vm 选项 -Djava.util.Arrays.useLegacyMergeSort=true
【发布时间】:2022-01-03 11:24:24
【问题描述】:

在我的应用程序中运行 junit-test 时出现此错误。后来我发现是因为declared field size

java.lang.IllegalArgumentException: Comparison method violates its general contract!

at java.util.TimSort.mergeHi(TimSort.java:899)
at java.util.TimSort.mergeAt(TimSort.java:516)
at java.util.TimSort.mergeCollapse(TimSort.java:441)
...
org.mockito.internal.configuration.injection.PropertyAndSetterInjection.orderedInstanceFieldsFrom(PropertyAndSetterInjection.java:125)

我发现可能的解决方案是在 VM args 中添加此标志。
-Djava.util.Arrays.useLegacyMergeSort=true。但我想在 pom.xml 中添加

我提到了这个how to add VM args using pom xml,但它主要指的是 -X 标志,这里的合适位置是什么?

【问题讨论】:

  • 您是否考虑过更新 Mockito?
  • 不,我不能在这里更新 Mockito,它是一个很大的旧项目
  • 我用 maven 运行这个项目,@Kayaman,谢谢你的提问
  • systemPropertyVariables 适合你吗?见maven.apache.org/surefire/maven-surefire-plugin/examples/…
  • 没有@Lesiak,因为我没有使用 maven-surefire-plugin

标签: java maven junit mockito pom.xml


【解决方案1】:

我用surefire插件更新了pom.xml,并按照here的建议使用了argLine参数

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <argLine>-Djava.util.Arrays.useLegacyMergeSort=true</argLine>
    </configuration>
</plugin>

【讨论】:

    【解决方案2】:

    有两种方法可以配置 [surefire 插件中的系统属性] (https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html)

    选项 1:systemPropertyVariables

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
            <systemPropertyVariables>
                <java.util.Arrays.useLegacyMergeSort>true</java.util.Arrays.useLegacyMergeSort>
            </systemPropertyVariables>
        </configuration>
    </plugin>
    

    选项 2:argLine

    部分系统属性必须在分叉虚拟机的命令行中设置,在虚拟机启动后无法设置。这些属性必须添加到 Surefire 插件的 argLine 参数中

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
            <argLine>-Djava.util.Arrays.useLegacyMergeSort=true</argLine>
        </configuration>
    </plugin>
    

    升级您的依赖项

    另一方面,我真的认为正确的解决方案是将 Mockito 升级到 2.1 或更高版本

    2.1 于 2016 年 10 月发布,包含对您的问题的修复:Make PropertyAndSetterInjection field sorting consistent #176

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 2015-01-16
      • 2023-03-26
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 2018-10-19
      相关资源
      最近更新 更多