junit4testcasespring-junit

The order of the test methods in a Test Suite by JUnit4Spring


Is the order of the test methods in a Test Suite by JUnit4Spring reliable?

Otherwise, is there anyway to enforce the order?

Thank you a lot.


Solution

  • The SpringJUnit4ClassRunner is great!
    If you are using JUnit 4.11, you can also use the @FixMethodOrder annotation
    See example:

    import org.junit.runners.MethodSorters;
    
    import org.junit.FixMethodOrder;
    import org.junit.Test;
    
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class SampleTest {
    
        @Test
        public void firstTest() {
            System.out.println("first");
        }
    
        @Test
        public void secondTest() {
            System.out.println("second");
        }
    }