7

I am having a test suite which is having the following structure

TestClass1
      - testmethod1()
      - testmethod2()
      - testmethod3()
      - testmethod4() 
TestClass2
          - testmethod11()
          - testmethod22()
          - testmethod33()
          - testmethod44()

In the above structure i want to execute the testmethod4() as the final one. ie) executed at last. There is a annotation @FixMethodOrder which executes a method in order not the testclass. Is there any mechanism to maintain order in test class and testmethod together. With the @FixMethodOrder i can execute the method by renaming the name of the test method but i can't instruct junit to execute the test class as the final one(last one).

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • 8
    You shouldn't care about test ordering. If it's important, you've got interdependencies between tests, so you're testing behaviour + interdependencies, not simply behaviour. Your tests should work identically when executed in any order. – Andy Turner Mar 04 '17 at 18:34
  • My scenario is that the specific method is accessing updating the values in db. Before that i need to execute all the tests. Also i agree with your point but could you please tell me if there are any other possiblities? – Shriram Mar 04 '17 at 19:27
  • @Shriram If I am not getting you wrong. You need to executed `TestClass1` after all other test class and then also ensure to execute `testmethod4` to executed at last? – Naman Mar 04 '17 at 19:30
  • Thanks @nullpointer – Shriram Mar 04 '17 at 20:58
  • 1
    "tell me if there are any other possiblities" Put the data you need for a particular test case in the db as part of the test case setup. – Andy Turner Mar 04 '17 at 21:38
  • Normally I would agree with the comment about not caring about test ordering. But I've also run into situations where one has something like a state-changing bug in a large suite. In cases like these a predictable ordering can help you find where your suite went off the rails. – Steve B. Mar 12 '18 at 20:14
  • Not only that, with Spring boot for example, the test context is cached and reused so it's faster to run tests with same context together. – Aníbal Jul 21 '22 at 23:26
  • In many cases it is important to create sequential tests and not independent ones, especially when you want to maintain consistency in a test data flow, such as creating records that depend on others, which cannot be created multiple times per theme. security, for example, test the creation of bank accounts. In many cases, one record depends on another and there is too much redundancy in the processes to carry out a simple validation. Parallel multitasking of tests can hurt rather than help. Unfortunately it's not something Junit developers understand. – e-info128 Aug 27 '23 at 02:19

2 Answers2

10

Though quoting @Andy again -

You shouldn't care about test ordering. If it's important, you've got interdependencies between tests, so you're testing behaviour + interdependencies, not simply behaviour. Your tests should work identically when executed in any order.

But if the need be to do so, you can try out Suite

@RunWith(Suite.class)

@Suite.SuiteClasses({
        TestClass2.class,
        TestClass1.class
})
public class JunitSuiteTest {
}

where you can either specify

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestClass1 {

    @AfterClass
    public void testMethod4() {

and then take care to name your method testMethod4 as such to be executed at the end OR you can also use @AfterClass which could soon be replaced by @AfterAll in Junit5.

Do take a look at Controlling the Order of the JUnit test by Alan Harder

Naman
  • 27,789
  • 26
  • 218
  • 353
  • The link is broken. Takes you to "Welcome to Oracle Blogs. The content that you're looking is no longer on this page." – user3217883 Oct 08 '18 at 14:47
  • 1
    Controlling the order of JUnit tests in the Wayback machine https://web.archive.org/web/20131216022901/http://blogs.oracle.com/mindless/entry/controlling_the_order_of_junit – Konstantin Pelepelin Nov 27 '18 at 18:29
  • 1
    Of course you must care about test ordering, test must fail as fast as possible, I always put in first tests related to fresh code changes. – Thomas Decaux Jun 20 '19 at 15:10
  • OP literally mentioned suite, and mentioned ordering test methods within classes, so this is redundant – Keith Tyler Oct 16 '20 at 22:15
  • Whats the problem with "test ordering". There is something called "integration tests". in which we might want to test a large workflow,. so test ordering makes sense. – RamPrakash May 19 '23 at 00:50
  • But in the test class can not initialize with spring core, can not use Autowired, services or `TestRestTemplate`. If you use `SpringBootTest` annotation the spring test call two times same test, one for `SpringBootTest` and other for the Suite. – e-info128 Aug 27 '23 at 02:58
-3

@shiriam as @Andy Turner already pointed out, the order of your tests shouldn't come in question when running the tests.

If you have a routine that you want executed before doing any tests, you could use a static block of code in one of the classes.

Think of something like this:

class TestBootstrap {
  // singleton instance
  private static final instance;
  private boolean initialized;

  private TestBootstrap(){
      this.initialized = false;    
  }

  public static TestBootstrap getInstance(){
       if (instance == null){
           instance = new TestBootstrap()
       }

  }
  public void init(){
      // make the method idempotent
      if (!initialzed){
         // do init stuff
         initialized = true;
      }
  }

  public boolean isInitialized(){
     return initialized;
  }

}

Then in your tests use something like this:

class TestClass1{
    @BeforeClass
    public void setup(){
         TestBootstrap.getInstance().init();
    }


    @Test
    public void testmethod1(){
        // assertions
    }

    // ....

}

class TestClass2{
    @BeforeClass
    public void setup(){
         TestBootstrap.getInstance().init();
    }

    @Test
    public void testmethod11(){
        // assertions
    }

    // ...
}

By using the singleton instance for doing the setup for the tests you ensure that you perform the initialization of your test environment only once, independently of the order in which the test classes are executed.

Community
  • 1
  • 1
marius_neo
  • 1,535
  • 1
  • 13
  • 28