3

I have a Test package like in this order (Using Junit4) in test package of Spring boot Test -Test1.java -Test2.java -Test3.java

Now when I am running the Test file in IntelliJ its going totally fine with no issue but when I am running mvn test its failing and I checked the error which can only occur if Test2.java has run before Test1.java. I know that test cases inside a Test class will not maintain any order but is it true for Test packages as well ?

[ERROR] Please refer to /Users/s0r0e4g/Desktop/SPEED CHANGE/Config_ClubItemBenchMark/oia-configuration/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
saket
  • 41
  • 2
  • Unit tests should not be required to run in a particular order otherwise those unit tests are integration tests ... furhtermore in general you can not define a particular order only by using JUnit Jupiter (aka JUnit 5) ... please show your pom file and examples of your tests... – khmarbaise May 25 '23 at 09:45

1 Answers1

0

and I checked the error which can only occur if Test2.java has run before Test1.java. I know that test cases inside a Test class will not maintain any order but is it true for Test packages as well ?

I have faced the exact same scenario, and indeed the test files can and will be executed in different order.

What we ended up doing although not a best practice is the solution provided here. This might not be the best practice since it is not a good practice to have order dependency of test files but in case you find yourself in a huge project that fails for this reason and you can't rewrite most of the test code it makes sense to use this as a workaround.

So in your case I would use the

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <runOrder>alphabetical</runOrder>
    </configuration>
</plugin>

and I would rename Test2.java to Test1.java and also rename Test1.java into Test2.java. This will ensure their execution ordering in different scenarios and machines.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47