• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Maintaining the order of JUnit3 tests with JDK 1.7

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have thousands of tests, it might be easier to just implement your own JUnit that preserves order
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic