0

If I am not mistaken, Junit 5 allows creation of custom method orderers so that the order in which test methods are executed can be custom but I could not find something equivalent to this in Junit 4, there seems to be ways to order tests in alphabetical order or using JVM ordering etc. but nothing that allows for custom execution. Is this something possible?

note: I am aware of the fact that fixing test order is a bad practice however I am looking into this for research, not software development.

alperenn
  • 21
  • 2
  • 1
    Does this answer your question? [How to run test methods in specific order in JUnit4?](https://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4) – Alexey R. May 31 '22 at 17:43
  • @AlexeyR. I also saw that thread however it seems to have just 3 ways to sort the test methods and does not fully implement the flexibility of junit 5 i.e. it does not let me define a comparator etc. – alperenn Jun 01 '22 at 11:42

1 Answers1

0

Starting JUnit 4.11, we have the annotation @FixMethodOrder and MethodSorters.class supporting the facility of setting an order for a test’s execution.

The package org.junit.runners.* needs to be imported to include the class MethodSorters. This class decides how the test cases need to be ordered. MethodSorters have three enum values.

MethodSorters.DEFAULT
MethodSorters.JVM
MethodSorters.NAME_ASCENDING

Please take a look at JUnit Test Execution Order: Order Of Tests JUnit 4 Vs JUnit 5 for details.

Eskandar Abedini
  • 2,090
  • 2
  • 13
  • Hello, thank you for your suggestion and I also did indeed come across this solution however this doesn't seem to be giving the flexibility that JUnit 5 gives since I cannot define how the tests will be sorted and only gives 3 predetermined ways to sort methods. – alperenn Jun 01 '22 at 11:41
  • Please take a look at [How to run test methods in specific order in JUnit4?](https://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4), there are some discussion about this subject. – Eskandar Abedini Jun 01 '22 at 12:00