-1

Is there a way to make tests for specific classes run in sequential order [JUnit5]?. I am using the sure-fire plugin when building the project and my tests on some classes fail, while when I run them in IntelliJ class by class they all pass. I have tried using the Order annotation, but the tests still fail which tells me that this annotation doesn't make tests run in sequential order. I want to make two of my test classes to execute their tests sequentially, while others do it in parallel, but I couldn't find proper solution on how to do that?

P.S. I have added a configuration in the maven sure-fire plugin that runs one JVM process, but this is not the desired behavior that I want to have.

Any help would be appreciated. Thanks in advance!

  • 3
    There is a tweak consisting on adding the configuration alphabetical to your surefire plugin, then you rename all your tests to respect the order. But it is a very bad idea. Your tests shouldn't be inter-dependent, if they are, the problem is not running them in sequence but understanding why they're not independent and make them so. – Matteo NNZ Sep 15 '22 at 15:40
  • 2
    Use the `@TestMethodOrder` annotation. See [§2.10.1 Method Order](https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-execution-order-methods) of the JUnit 5 user guide. – Slaw Sep 15 '22 at 16:01

1 Answers1

3

As described in the JUnit user guide, you can use the @TestMedhodOrder annotation to control the ordering of tests during runtime. For example, to use @Order annotations on the test methods, you would apply @TestMethodOrder(OrderAnnotation.class) at the test class level.

Having said that, you really, really, really should consider that as a last resort. Unless you explicitly and knowingly (fully understanding why) wrote the tests to require a specific ordering, you will just be masking some underlying problem in either your tests or the code they are testing.

Do yourself a favor and do your best to figure out why they fail, instead of putting the band-aid of forced ordering on the problem.

E-Riz
  • 31,431
  • 9
  • 97
  • 134