0

I got this error while running a junit-test in my application. Which I later found out because of 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)

I found that probable solution would be adding this flag.
-Djava.util.Arrays.useLegacyMergeSort=true in VM args. But I wanted to add in pom.xml

I referred to this how to add VM args using pom xml but it refers mostly for -X flags, what would be an appropriate placement in here?

Lesiak
  • 22,088
  • 2
  • 41
  • 65
Saurav
  • 614
  • 7
  • 13
  • 3
    Have you considered updating Mockito instead? – Kayaman Jan 03 '22 at 11:41
  • No I cannot update Mockito here, its a big old project – Saurav Jan 03 '22 at 11:43
  • I run the project with maven, @Kayaman, thanks for asking – Saurav Jan 03 '22 at 12:29
  • Do `systemPropertyVariables` work for you? See https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html – Lesiak Jan 03 '22 at 12:29
  • Did not @Lesiak because I am not using maven-surefire-plugin – Saurav Jan 03 '22 at 12:34
  • What are you using then? Note that Surefire plugin is enabled by default, even if you don't configure it in your pom. Check output of `mvn help:effective-pom` to verify my statement. See also: https://www.baeldung.com/maven-surefire-plugin – Lesiak Jan 03 '22 at 12:42
  • Thanks for the reference link @Lesiak, I added Surefire back in pom.xml, it was not already present. And now it reflected in the test execution – Saurav Jan 04 '22 at 09:25

2 Answers2

0

I updated the pom.xml with the surefire plugin and used argLine parameter as suggested here

<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>
Saurav
  • 614
  • 7
  • 13
0

Trere are 2 ways to configure [System Properties in surefire plugin] (https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html)

Option 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>

Option 2: argLine

Some system properties must be set on the command line of the forked VM, and cannot be set after the VM has been started. These properties must be added to the argLine parameter of the Surefire plugin

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

Upgrade your dependencies

On the other hand, I really think that the right solution is to upgrade Mockito to 2.1 or later

2.1 was released in October, 2016, and contains the fix to your issue: Make PropertyAndSetterInjection field sorting consistent #176

Lesiak
  • 22,088
  • 2
  • 41
  • 65