• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maintaining the order of JUnit3 tests with JDK 1.7

 
Prince Manchanda
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Marco
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34664
366
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have thousands of tests, it might be easier to just implement your own JUnit that preserves order
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic