Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JUnit test cases to run in sequential order?

I am using JUnit4.

I have a set of test methods in a test case.

Each test method inserts some records and verify a test result and finally delete the records inserted.

Since the JUnit run in parallel, test methods fail because of some records present during the execution of previous test method. This happen only in my colleague machine(Windows 7), not in my machine(Cent OS 6).

What we need is that the test methods have to pass in all our machines.

I have tried clearing the records in the Setup() method but again it works only on my machine. Is there any option available in JUnit to make the test methods to run in a uniform sequential order ?

Thanks,

like image 378
Athiruban Avatar asked Mar 12 '12 13:03

Athiruban


People also ask

Is JUnit sequential?

Basically, JUnit doesn't support ordering and the tests should be able to be run in any order.

Are JUnit tests run concurrently?

Once parallel test execution property is enabled, the JUnit Jupiter engine will execute tests in parallel according to the provided configuration with declared synchronization mechanisms.

Can we set priority in JUnit?

In general, you can't specify the order that separate unit tests run in (though you could specify priorities in TestNG and have a different priority for each test). However, unit tests should be able to be run in isolation, so the order of the tests should not matter.


3 Answers

MethodSorters is a new class introduced after Junit 4.6 release. This class declared three types of execution order, which can be used in your test cases while executing them.

  1. NAME_ASCENDING(MethodSorters.NAME_ASCENDING) - Sorts the test methods by the method name, in lexicographic order.

  2. JVM(null) - Leaves the test methods in the order returned by the JVM. Note that the order from the JVM my vary from run to run.

  3. DEFAULT(MethodSorter.DEFAULT) - Sorts the test methods in a deterministic, but not predictable, order.

.

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

//Running test cases in order of method names in ascending order

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderedTestCasesExecution {

    @Test
    public void secondTest() {
        System.out.println("Executing second test");
    }

    @Test
    public void firstTest() {
        System.out.println("Executing first test");
    }

    @Test
    public void thirdTest() {
        System.out.println("Executing third test");
    }
}

Output:

Executing first test
Executing second test
Executing third test

Reference: http://howtodoinjava.com/2012/11/24/ordered-testcases-execution-in-junit-4/

like image 78
Ranga Reddy Avatar answered Nov 14 '22 04:11

Ranga Reddy


JUnit 4.11 now supports specifying execution order using @FixMethodOrder annotation.

like image 42
Aleksandr Dubinsky Avatar answered Nov 14 '22 04:11

Aleksandr Dubinsky


Ordering of tests is not guaranteed in JUnit.

The reason for this is that unit tests are meant to be atomic - all of the setup should happen in the setup / tear down methods, but not by other tests.

Consider moving the code that inserts data into another helper class that can be called by both the test that's inserting and the class that needs to verify, and calling that class in your @Before methods.

You should also consider a mocking solution (eg Mockito) as opposed to hitting the database directly if you can - mocking will go a long way to ensuring that your tests are nice and isolated, and, as a nice side benefit, usually help point out where you could use some refactoring.

like image 25
Roy Truelove Avatar answered Nov 14 '22 04:11

Roy Truelove