my dog learned polymorphism
The moose likes Testing and the fly likes Maintaining the order of JUnit3 tests with JDK 1.7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Maintaining the order of JUnit3 tests with JDK 1.7" Watch "Maintaining the order of JUnit3 tests with JDK 1.7" New topic

Maintaining the order of JUnit3 tests with JDK 1.7

Prince Manchanda
Ranch Hand

Joined: Jun 25, 2001
Posts: 52
Do we have any way (apart from using suite() method) to ensure that the methods of a JUnit are returned in a specific order.

We have a quite a good number of JUnits(running into thousands). Many of them (don't have the exact numbers) are written in such a way that tests in a JUnit are dependent on each other. No doubt this is a bad design practice but the application is quite old.

Till now we were using JDK 1.6 that maintained the order of the methods in same order as they appear in the source code. However, we need to move to JDK 1.7. But JDK 1.7 doesn't guarantee any ordering in which the methods are returned. This results in failure of the JUnits that have dependency.

Using the suite method to maintain the order is not suitable as that would mean modifying all the JUnits.

Thanks and Regards
P Manchanda
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294


I think JUnit 3 uses reflection to find test methods and according to the Java 7 API of class Class declared methods are NOT returned in any particular order. So it probably depends on the vendor and version of your JDK but it's surely no portable solution to expect JUnit to find the methods in sorted order.

But I must admit I'm more familiar with TestNG. TestNG offers you a way to describe dependencies between test methods via annotations. Would it be possible to upgrade the tests to use TestNG? Of course that would also mean to modify all tests as with the suite() method.

Unfortunately I can't imagine an easier way. I guess that's the price you have to pay for using dependent tests (a bad idea in my opinion). Maybe someone else can come up with a better idea.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

JUnit has never guaranteed an ordering for the tests and dependent tests have always been an anti-pattern. Fixing the tests is your best bet. You could use Java 6 for your tests in the meantime.

I don't see how converting to TestNG would help here as you would still need to understand the test dependencies.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

Jeanne, as I wrote I don't like the idea of dependent tests either. I just suggested to use TestNG because it at least allows you to make the dependencies between tests explicit so you don't have to rely on the implementation details of a specific JDK.

Of course I'd recommend to remove the dependencies in tests completely as a clean solution, even if it requires more work.
Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2746

If you have thousands of tests, it might be easier to just implement your own JUnit that preserves order
I agree. Here's the link:
subject: Maintaining the order of JUnit3 tests with JDK 1.7
It's not a secret anymore!