I know this is bad practice, but it needs to be done, or I’ll need to switch to testng. Is there a way, similar to JUnit 3’s testSuite, to specify the order of the tests to be run in a class?
I know this is bad practice, but it needs to be done, or I’ll
Share
If you’re sure you really want to do this: There may be a better way, but this is all I could come up with…
JUnit4 has an annotation:
@RunWithwhich lets you override the default Runner for your tests.In your case you would want to create a special subclass of
BlockJunit4ClassRunner, and overridecomputeTestMethods()to return tests in the order you want them executed. For example, let’s say I want to execute my tests in reverse alphabetical order:Running this test produces:
For your specific case, you would want a comparator that would sort the tests by name in the order you want them executed. (I would suggest defining the comparator using something like Google Guava’s class
Ordering.explicit("methodName1","methodName2").onResultOf(...);where onResultOf is provided a function that converts FrameworkMethod to its name… though obviously you are free to implement that any way you want.