1

I have a problem with Idea 14 and JUnit. I can't run @BeforeClass and @AfterClass methods in proper order (before all test and after all test). Every time order is different. I tried to reinstall IDEA, delete all settings but nothing works. Please help. This is example of my test code:

package com.rent.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import static org.junit.Assert.*;
import org.junit.Test;

public class testnewTest {
    static int num;
    static int num1;

    @BeforeClass
    public static void OnceExecutedBeforeAll() {
        System.out.println("@BeforeClass: onceExecutedBeforeAll");
        num = 15;
        num1 = 16;
    }

    @AfterClass
    public static void after() throws Exception {
        System.out.println("End");
    }

    @Test
    public void testLogin() throws Exception {
        System.out.println("test");
        assertEquals(15, num);
    }

    @Test
    public void testGetOrdersDate() throws Exception {
        System.out.println("test2");
        assertEquals(16, num1);
    }
}

This is output:

 test2
 @BeforeClass: onceExecutedBeforeAll
 test
 End
Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
Gekster
  • 91
  • 1
  • 7
  • Works fine in my IDEA 14.1.3 with JUnit 4.12. Which versions are you using? – Mureinik Sep 16 '15 at 17:14
  • In junit the order of execution is not defined, check the link below http://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4 – Rafael Guillen Sep 16 '15 at 17:23
  • @RafaelGuillen: That's more for the individual tests. Order isn't guaranteed but it's deterministic. This concerns the `@BeforeClass` and `@AfterClass`. – Makoto Sep 16 '15 at 19:24
  • Version of IDEA: 14.1.4 – Gekster Sep 18 '15 at 18:26

1 Answers1

3

What you're likely observing is the fact that the output is not always going to be synchronous in the terminal. The test themselves are running in the correct sequence.

If they weren't, then you would have failures in test2 given that it would have appeared that your @BeforeClass method fired afterwards.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Sometimes test tries to run tests before @Beforclass methods end and test failure because of lack of proper context. – Gekster Sep 18 '15 at 18:25