To be short - I' just wrote a singleton class. Like the all singletons consists my singleton of a private constructor and a static field containing a predefined amount of objects of the class.
The case is - the singleton class has to be initialized using a JSON file and the expected behavior is it throwing an exception when no files from a given list are present.
The problem is that apparently IntelliJ (or maybe just JUnit) is loading all test classes and then executing them one by one in one program in a random order. This means that by the time the class which test the wrong initialization has been reached, the singleton already has been initialized. This means that every time I run all the tests at once I'll have one test which did not pass and when I run them one by one manually it'll result in all the tests passing.
Is there a way to force IntelliJ to run all the test classes separated?
Edit
I don't know how this could change anything, but lets assume it will. Here is a sample code with the exact same behavior. It's an IDE question, not a java speciffic one.
public class Singleton {
private static Singleton singleton = new Singleton();
public static Singleton getInstance() {
return singleton;
}
private Singleton() {
if (!new File("maybeIExist.txt").exists())
throw new ExceptionInInitializerError();
}
public void doStuff() {
System.out.println("stuff");
}
}
And of course the tests:
public class SingletonTestOne {
@Test(expected = ExceptionInInitializerError.class)
public void test() throws Exception {
Singleton.getInstance();
}
}
And the second one:
public class SingletonTestTwo {
@Before
public void before() throws Exception {
//code creating file
}
@Test
public void test() throws Exception {
Singleton.getInstance();
doStuff();
}
@After
public void after() throws Exception {
//code deleting file
}
}