3

Hi I have a requirement to run one of my test methods after certain method completed. Even though tests should be independent as per How to run test methods in specific order in JUnit4?.

There is a another hack for this is to use the

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

But what I want is I need to run the tests in the order same as in the file. If it cannot be done, then I just have to live with Method Name sorters

Community
  • 1
  • 1
Tharsan Sivakumar
  • 6,351
  • 3
  • 19
  • 28
  • 3
    the order of declaration, is to my knowledge not available during runtime. – raphaëλ Jul 08 '16 at 12:10
  • 3
    IMHO this is not a good question because it is clearly against the spirit of Unit Testing to rely on a specific order. A much better question would be to ask how to make your tests independent given your requirements. – martinhh Jul 08 '16 at 12:23
  • Tests should be run independently, and pre test actions should happen via the @Before annotation – UserF40 Jul 08 '16 at 12:33
  • Just a friendly note that if you find one of the answers to your question acceptable, feel free to [accept it](http://stackoverflow.com/help/accepted-answer) if you want to. – Sam Brannen Sep 10 '16 at 10:48

2 Answers2

2

Since Java 7, the JDK no longer guarantees the order in which methods in a class will be returned when looked up via reflection (as a framework like JUnit must do).

Thus, the answer to your question is: No, it is not possible to ensure that test methods are executed in the order in which they are declared in source code.

As you noticed, the only alternative to potentially random ordering is @FixMethodOrder(MethodSorters.NAME_ASCENDING) in JUnit 4 on JDK 7+.

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
1

There is a very crucial point, why you shouldn't start to invent such requirements.

The idea of unit tests is that each test ... tests a different aspect of your class under test. Aspects that are independent of each other. That is the one side. The other is: unit tests exist to help you to determine problems in your production as quickly as possible.

Meaning: when all of a sudden, a unit test is failing (because somebody changed something somewhere), then you want to find the root cause of this problem as soon as possible. Anything that prevents you from getting to that goal makes your unit test less valuable to you!

Things that make it harder for you to find the foot cause would be:

  1. extensive "externalized" setup - when you start using (too much) inheritance for your test cases
  2. complicated relations between your test methods (like: your idea)

Long story short: unit tests are not meant to be run in any specific order. So, instead of spending a lot of time pushing JUnit into a corner where it doesn't want to be ... step back for a second, and reconsider why you think you need things to be that way.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • FYI: the original poster did not state anything about "unit tests". So, although your arguments are valid for unit testing, they do not apply to all forms of testing such as integration testing or scenario testing where ordering can in fact make a huge difference. – Sam Brannen Jul 09 '16 at 13:36