14

guys! I have a new question for you. I'm adding some data to cache using different cache managers and I'm facing the problem with it. I'm doing it using junit and spring. When I run the test, the test methods are executed randomly, but I need them to be executed in order. How to do it?? Here is some code and a proving console output:

Service class:

@Service("HelloCache")
public class CacheServiceImpl implements CacheInterface {
    @Autowired
    @Qualifier("memcachedClient")
    private MemcachedClient mBean;

    public void Add(String key, Object object) {
        mBean.set(key, 12, object);
    }

    public void Get(String key) {
        mBean.get(key);
    }

    public void Delete(String key) {
        mBean.delete(key);
    }    
}

Here is the test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "file:src/main/java/spring.xml")
public class UsingMemcachedTest extends TestCase {
    @Autowired
    @Qualifier("HelloCache")
    private CacheInterface emcached;
    private byte[][] i = new byte[2500][3000];
    private String key = "j";
    @Test
    public void testAddBulkObjects() {
        System.out.println("");
        System.out.println("This is async BULK adding test");
        long time = System.currentTimeMillis();
        for (int k=1; k<=1000; k++) {
            emcached.Add(key+k, i);
        }
        long time2 = System.currentTimeMillis();
        long timeE=time2-time;
        System.out.println("Vremya add BULK objects: " + timeE);
        System.out.println("");
    }

    @Test
    public void testGetBulkObjects() {
        System.out.println("");
        System.out.println("This is getting BULK objects test");
        long time = System.currentTimeMillis(); 
        for (int k=1; k<=1000; k++) {
            emcached.Get(key+k);
        }
        long time2 = System.currentTimeMillis();
        long timeE=time2-time;
        System.out.println("Vremya Get object: " + timeE);
        System.out.println("");
    } 

    @Test
    public void testDeleteBulkObjects() {
        System.out.println("");
        System.out.println("This is deleting BULK objects test");
        long time = System.currentTimeMillis();
        for (int k=1; k<=1000; k++) {
            emcached.Delete(key+k);
        }
        long time2 = System.currentTimeMillis();
        long timeE=time2-time;
        System.out.println("Vremya delete object: " + timeE);
        System.out.println("");
    }

And the output:

This is deleting BULK objects test
Vremya delete object: 137


This is getting BULK objects test
Vremya Get object: 703


This is async BULK adding test
Vremya add BULK objects: 87681

Please, HELP!! =)

PAcan
  • 871
  • 2
  • 15
  • 32
  • 3
    Why do you want them executed in order? Tests should be able to run in any order, IMO. You could always do it the old-fashioned way and make a TestSuite. – Dave Newton Apr 02 '13 at 00:26
  • I need to do it, because I'm adding some data to cache, then getting it from cache and only then deleting it. That's why. – PAcan Apr 02 '13 at 00:51
  • @DaveNewton, so , "cancelOrder" should be executed first and then "orderProduct" next. Is it a workflow? Remember, your advice is applicable for unit tests. not everywhere. There is something called integration tests. – RamPrakash May 19 '23 at 00:53
  • @RamPrakash Look at the tests again. These are unit tests. These unit tests are executed in random order because they’re unit tests. A unit test should not be used as data setup for following unit tests. Perhaps you meant to tell the OP that. – Dave Newton May 19 '23 at 01:17

3 Answers3

49

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
9

JUnit makes no promises regarding the order in which your tests are run. The order WILL be different depending on the environment and context in which the tests are run.

For this reason (and others) it is considered very bad test design for ordering to affect the behaviour of your tests. You can use @Before go give you a clean slate to work with and then do any set up for a particular test as part of that test.

The accepted answer for the following question gives a good explanation and links to some useful resources: How to run test methods in specific order in JUnit4?

Community
  • 1
  • 1
Aurand
  • 5,487
  • 1
  • 25
  • 35
4

JUnit 4.11 added some cool stuff that allows you to control the Test Execution Order

Hiro2k
  • 5,254
  • 4
  • 23
  • 28