1

Is there any way to execute a specific test case first in a test class irrespective of the order of the test case present in the class ?

Example:

public class TestExample {

    @Test
    public void test1() {}

    @Test
    public void test2(){}

}

Can we execute test2 first ? do we have any @Order something like that ??

Thanks!!

Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56

5 Answers5

4

Ordering tests is generally considered to be a bad idea because it implies that the tests are not independent. Non-independent tests can cause multiple test failures (for instance if you have several tests that rely on shared state set up by another test, if any test along the way fails to clean up then the successive ones are compromised), and it can be hard to work out what went wrong. The ideal is that one problem should cause one test failure.

Anyway, I had thought because of the foregoing JUnit wouldn't support this but apparently it does, see this answer. (+1 for the other answers that pointed this out.) One easy alternative to making your own MethodSorter would be to use an existing one, NAME_ASCENDING, and change the method name of the one you want to go first so it will sort highest.

BTW this feature isn't totally an abomination; for instance, if you're in a situation where you suspect that a dependency slipped in, it's good to have a way to tweak the order of the tests to figure out what's going on.

Also there's a @BeforeClass annotation if you have something you want executed before any test is executed.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • yes nathan, I accept that isnt a best practice to have the dependent order of execution. Got into a situation where I need to maintain order. Thanks for the link and answer !! – Vinay Veluri Nov 21 '13 at 17:31
2

Even though relying on the order of your test methods is I think a bad practice, you could still use the @FixMethodOrder(MethodSorters.NAME_ASCENDING) annotation (if it fits).

If this is not enough, you could also look at this post.

Community
  • 1
  • 1
benjamin.d
  • 2,801
  • 3
  • 23
  • 35
2

The idea of JUnit and most unit testing frameworks is that all tests are independent (that is none depend on another) and so can be run in any order. I think some do this in a random order.

I think that needing to run one first shows you have a problem in your setup. If something needs to be done for all or before tests then consider implementing that in a @setUp method

If you need the tests to run in order because you have dependencies etc then I would look at TestNG JUnit from version 4.11 has the @FixMethodOrder(MethodSorters.NAME_ASCENDING) assertion, so you can make the first method the first in alphabetical order

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
-1

I use this pattern:

public class TestExample {

    @Test
    public void test1() {
        firstTest();

        ...rest of test1...
    }

    @Test
    public void test2(){
        firstTest();    
    }

    private static boolean ranCommonCode = false;

    private void firstTest() {
        if( ranCommonCode ) return;
        ranCommonCode = true;

        ...code of test2...
    }
}
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
-2

Why not?

    public void test1(){
    }    

    public void test2(){
    }    

    @Test
    public void test() {
        test2();
        test1();
    }
mvb13
  • 1,514
  • 3
  • 18
  • 33