JUnit @FixMethodOrder Annotation Example

0

The order of execution JUnit tests within a class is not guaranteed and can even change from run to run. Since JUnit 4.11, the order is deterministic, it compares hashcode of the test methods to decide the order, if hashcode is equal, will rely on the ascending lexicographic sort order.
JUnit provides @FixMethodOrder annotation to have a control on the order of execution of unit tests. One can chose the order of execution from the below strategies:

  1. Deterministic, based on hashcode. This is the default order.
  2. Ascending by method name
  3. JVM order. This more or less returns random order.

Deterministic Order of Tests

By default the order is deterministic and not random. Below test class doesn’t have the @FixMethodOrder annotation.

TestWithoutFixMethdOrderExample:

package com.javarticles.junit; 

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

public class TestWithoutFixMethdOrderExample {
    @Rule
    public TestName testName = new TestName();

    @Test
    public void apple() {   
        printDetails();
    }    
    
    @Test
    public void brownies() {
        printDetails();
    }
    
    @Test
    public void cake() {
        printDetails();
    }
    
    private void printDetails() {
        System.out.println(testName.getMethodName() + ", hash(" + testName.getMethodName().hashCode() + ")");
    }
}

Output:

cake, hash(3045944)
apple, hash(93029210)
brownies, hash(384041441)

One will get the same result even if @FixMethodOrder is present as the default behavior is deterministic.

FixMethodOrderByDeterministicExample:

package com.javarticles.junit; 

import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

@FixMethodOrder
public class FixMethodOrderByDeterministicExample {
    @Rule
    public TestName testName = new TestName();

    @Test
    public void apple() {   
        printDetails();
    }    
    
    @Test
    public void brownies() {
        printDetails();
    }
    
    @Test
    public void cake() {
        printDetails();
    }
    
    private void printDetails() {
        System.out.println(testName.getMethodName() + ", hash(" + testName.getMethodName().hashCode() + ")");
    }
}

Output:

cake, hash(3045944)
apple, hash(93029210)
brownies, hash(384041441)

Unit Tests Ordered by Name

You can chose to sort the test methods by the method name, in lexicographic order. Sett the value of @FixMethodOrder to MethodSorters.NAME_ASCENDING.

FixMethodOrderByNameExample:

package com.javarticles.junit; 

import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FixMethodOrderByNameExample {
    @Rule
    public TestName testName = new TestName();
    
    @Test
    public void apple() {   
        printDetails();
    }    
    
    @Test
    public void brownies() {
        printDetails();
    }
    
    @Test
    public void cake() {
        printDetails();
    }
    
    private void printDetails() {
        System.out.println(testName.getMethodName());
    }
}

Output:

apple
brownies
cake

Unit Tests Ordered By JVM

You may leave the order of execution to JVM which is random in nature and the order may vary from run to run. In order to chose random order, set the value of @FixMethodOrder to MethodSorters.JVM.

FixMethodOrderSortByJVMMethodExample:

package com.javarticles.junit;

import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.JVM)
public class FixMethodOrderSortByJVMMethodExample {
    @Rule
    public TestName testName = new TestName();
    
    @Test
    public void apple() {   
        printDetails();
    }    
    
    @Test
    public void brownies() {
        printDetails();
    }
    
    @Test
    public void cake() {
        printDetails();
    }
    
    private void printDetails() {
        System.out.println(testName.getMethodName());
    }
}

Output:

cake
brownies
apple

Download the source code

This was an example about JUnit @FixMethodOrder annotation.

You can download the example here: junitFixMethodOrderExample.zip
Share.

Comments are closed.