I have a test class in java and there are several methods annotated by @Test in it, somehow, i want to Junit run method A before method B when i run the whole tests. Is it possible or necessary?
5 Answers
This sort of dependency on test methods is bad design and should be avoided. If there is initialization code in one test method that needs to be done for the next, it should be factored out into a setUp method.

- 26,325
- 16
- 80
- 110
-
2
-
3There is a good use case for this, though. It might be bad to have tests do set-up for one another, but at times we can say "if *this* test fails then there is no point in running *these other* tests, because they will definitely fail too". In this case, it would save time to be able to specify dependencies such that they can be skipped. – Elias Vasylenko Jul 21 '15 at 20:05
-
What makes you think dependency-ordering tests is "bad design"? I would say it's bad design to put all your "setup" in a single setup function, because some of the setup itself might be something you want to test and you'll end up with a monolithic function that will be harder to interpret when it fails. See TestNG for a proper implementation of test dependencies: http://testng.org/doc/documentation-main.html#dependent-methods. – Joshua Richardson Nov 01 '17 at 19:23
The problem I have with this is reporting. If you WANT/NEED to see if each test method fails or passes then you're SCREWED.
I understand that you don't want one test to build upon previous tests, But regardless of that, there may be situations that you need it to do this (or you'll increase the complexity of the test by an order of magnitude).
Should the flow of tests in the code be up to the developer of the tests or the developer of the framework ?
Show JUnit test code to 10 java developers, and I'll be willing to bet most will assume that the tests (regardless of anything external) will be run in the order they appear in the test class.
Shouldn't THAT be the default behaviour of JUnit ? (Give me the option of telling it the order instead of JUnit figuring it out" on its own.)
Update: 2014-11-18 The newer version of JUnit supports method sorters
// This saves the tests in alphabetical order @FixMethodOrder(MethodSorters.NAME_ASCENDING)
I would think that you might be able to create your own method sorter if you "really" wanted do do your own specific order.

- 51
- 3
-
The fact that junit has a mind of its own is stupid.. Tell me what the best practice is, dont force SHIT down my throat.. – Siddharth Aug 07 '15 at 04:34
Tests should have independent order, but some times we have not what we want. If you have a large legacy project with thousands of tests, and they depends on their execution order, you will have many problems, when, for example you will try to migrate on java 7, because it will shuffle all tests.
You can read more about this problem here:

- 1,109
- 8
- 11
If it's only two methods then you'd need to wrap it in a single unit test that truly isn't order-dependent.
@Test
public void testInOrder() throws Exception {
testA();
testB();
}

- 607
- 4
- 6
-
1I already trid this but not satisfied, i think there might have other way to do this.Thanks anyway. – George Mar 07 '11 at 03:04
use the following to setup thing before and after tests
@Before
public void setUp() throws MWException {
}
@After
public void tearDown() {
}

- 328
- 3
- 9