JUnit test method ordering

Junit until version 4.10 uses the order of test methods in a test class as returned by the reflection API as the order of test method execution - Class.getMethods(). To quote the Javadoc of getMethods() api:

The elements in the array returned are not sorted and are not in any particular order.

thus the order of test method execution in a test class is not predictable. This in a way is good as it encourages us as developers to write test methods which can stand on its own and to not depend on the order of test method execution.
 
 
 
With version 4.11 of Junit the order of test method execution is not so unpredictable anymore, by default the order though not specified will be deterministic for every run. The order can further be enforced by adding a new annotation @FixMethodOrder to the test class with the following values:

1. @FixMethodOrder(MethodSorters.DEFAULT) – deterministic order based on an internal comparator

2. @FixMethodOrder(MethodSorters.NAME_ASCENDING) – ascending order of method names

3. @FixMethodOrder(MethodSorters.JVM) – pre 4.11 way of depending on reflection based order

Consider the following test case:

public class ATest {
 @Test
 public void b_test_1() {
  System.out.println('b_test_1 called..');
 }

 @Test
 public void r_test_2() {
  System.out.println('r_test_2 called..');
 }

 @Test
 public void z_test_3() {
  System.out.println('z_test_3 called..');
 }

 @Test
 public void l_test_4() {
  System.out.println('l_test_4 called..');
 }
}

Pre 4.11 running this test prints the following in my machine

Running testorder.ATest
r_test_2 called..
z_test_3 called..
b_test_1 called..
l_test_4 called..

With NAME_ASCENDING:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ATest

this is the output:

Running testorder.ATest
b_test_1 called..
l_test_4 called..
r_test_2 called..
z_test_3 called..

There is still no way to explicitly specify other custom sorting methods which is good as the purpose of providing a predictable order is just that – to make it predictable, not to use it to add dependencies between test methods.

Resource:

JUnit Wiki – https://github.com/KentBeck/junit/wiki/Test-execution-order
 

Reference: JUnit test method ordering from our JCG partner Biju Kunjummen at the all and sundry blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


8 − seven =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books