10

I wanted to choose the order to execute the JUnit tests. I have 4 classes with several test methods in it, my goal is to execute, for instance, method Y of class A, then method X from class B, and finally method Z from class A.

Would you help please?

Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74

9 Answers9

26

From version 4.11 you can specify execution order using annotations and ordering by method name:

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

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTest {

    @Test
    public void test1Create() {
        System.out.println("first");
    }

    @Test
    public void test2Update() {
        System.out.println("second");
    }
}

See JUnit 4.11 Release Notes

Lorenzo Conserva
  • 1,026
  • 11
  • 9
22

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. This is a bad practice. If you need the tests to be in a specific order, you should be rethinking your design. If you post specifics as to why you need the order, I'm sure we can offer suggestions.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • 2
    +1, although I don't think this advice is worded quite strongly enough. Unit tests should be able to run in isolation; that's fundamental. If yours aren't, you're doing something wrong. – Carl Manaster Apr 19 '10 at 17:37
  • Ok... I'm new at this... So the thing is: I'm using OpenKM (a Document Management System) and I have a class that tests Folders Manipulation, i.e., tests the creation, renaming and deletion a folder. So you're telling me that the creation, renaming and deletion must be all on the same method instead of being seperated and dependent on the previous method? – Miguel Ribeiro Apr 19 '10 at 21:08
  • Generally you would have a setup and tear down method that would put the folders in a known state. For example, the setup method could create a folder (and tear down would delete it if it still exists). Your delete and rename tests could operate on that folder. The test for create could verify that the folder was created properly in the setup method or it could create an entirely new folder (or subfolder of the folder created in the setup method). – Jeff Storey Apr 20 '10 at 00:03
4

The JUnit answer to that question is to create one test method like this:

  @Test public void testAll() {
       classA.y();
       classB.x();
       classA.z();
  }

That is obviously an unsatisfying answer in certain cases (where setup and teardown matter), but the JUnit view of unit testing is that if tests are not independant, you are doing something wrong.

If the above doesn't meet your needs, have a look at TestNG.

Yishai
  • 90,445
  • 31
  • 189
  • 263
2

The general remark/idea that testing can be done in any arbitrary order is too strong.

It really depends on what you are testing.

For example I am testing a server where we have a changePassword action.

I think it is obvious that the order of tests is critical. After changePassword, the old password does not work anymore, and before, it does.

I don't want to revert the server state after each test, too much work. I can do it one time after all tests have been completed.

nomail
  • 21
  • 1
2

Create a TestSuite and call the test methods in the desired order. @Yishai is right in that JUnit is designed so each test is independent. So if you are calling test methods that can be run independently then there should be no problem with creating a TestSuite to cover a scenario for a specific calling-order.

Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
0

If the previous answer is not satisfying I have noticed with the Sun JVM JUnit always seems to execute unit tests in the order of which they are defined. Obviously this is not a good idea to rely on this.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • Just FYI for future readers, this seems to be the case with JVMs before Java 7, but since Java 7 the order is non-deterministically random. But JUnit 4.11+ has limited ability to define the order using the FixMethodOrder annotation. – Joe Casadonte Dec 23 '13 at 20:58
0

This might be interesting to you: JExample

A different approach to testing with interdepentent tests.

Geniedesalpages
  • 418
  • 1
  • 3
  • 15
0

You can use @Order() annotation

0

You can do this like with @Order annotation

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MyTest {
     
    @Test
    @Order(1)
    @DisplayName("First")
    public void firstTest() {
        System.out.println("a");
    }
     
    @Test
    @Order(2)
    @DisplayName("Second")    
    public void secondTest() {
        System.out.println("b");

    }
  
    @Test
    @Order(3)  
    @DisplayName("Third")  
    public void thirdTest() {
        System.out.println("c");
    }
}

Output

a
b
c
Shahrear Bin Amin
  • 1,075
  • 13
  • 31