How to run JUnit5 tests in sequential order?

Viewed 40

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!

1 Answers

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.

Related