0

When testing in junit, I like to organize my various tests into files depending on what is being tested.

Although it is bad practice to run unit tests in a set order, it would be rather convenient for my purposes.

I know how to run tests in order in a given file, but I would like to be able to choose the order in which my files are run.

Lets say I have two files: fooTest.java and barTest.java.

How would I specify the order in which these get executed?

All help is appreciated.

  • Why would it be convenient? [Here](https://stackoverflow.com/questions/2669414/choose-order-to-execute-junit-tests) is a similar discussion. – Andrew S Jul 24 '17 at 19:33
  • My main reasoning behind wanting to run the files in order is that pretty much every file has the exact same setup. The setup is rather slow, and it would make sense to run it before every other test file to spare some time. – Demirhan Ozel Jul 24 '17 at 19:42
  • Why is it slow? Is it really a unit test with mocked dependencies, or is it actually an integration test? Perhaps a base class with an @Before and set a static boolean, and only execute if it's false. Or use a helper class to avoid extending. – Andrew S Jul 24 '17 at 19:51
  • I have my base class set to @ignore otherwise it gets run as a junit test and gets called a failure by junit. As for why it is slow, I am at a loss. I am very aware that the rhino js engine which htmlunit uses is known for being slow. – Demirhan Ozel Jul 24 '17 at 20:37
  • See https://stackoverflow.com/q/13723125/217324 – Nathan Hughes Jul 24 '17 at 23:15

1 Answers1

2

To have a specific order among test classes you may try to group your tests under a Suite:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
   TestJunit1.class,
   TestJunit2.class})
public class JunitTestSuite {   
}

Look at this answer as well.

Dmytro Maslenko
  • 2,247
  • 9
  • 16