Reputation: 20119
Are there any documented conventions for the order in which method types are placed within JUnit tests? I typically have the following order: @Before
, @Test
, @After
; I have also seen: @Test
, @Before
, @After
.
Example methods:
public class SandBoxTest {
SandBox sand;
@BeforeClass
public void classSetup() { }
@Before
public void given() { }
@Test
public void shouldTestThis() { }
// support method
private boolean doStuff() {
return true;
}
@Test
public void shouldTestThat() { }
@After
public void cleanUp() { }
@AfterClass
public void classCleanUp() { }
}
If there is a 'standard' convention, please provide references.
Upvotes: 2
Views: 83
Reputation: 1847
I had this discussion with one of my colleagues. I was arguing for the order @BeforeClass
, @Before
, @After
, @AfterClass
, and then all the @Test
, whereas he wanted the order to be @BeforeClass
, @Before
, all the @Test
, and then @After
, @AfterClass
.
My arguments for having the setUp and tearDown methods at the top were:
His argument for having the tests in between the setUp and tearDown methods was that it was the logical order, which also reflects the order in which the methods are excecuted.
We searched for a convention on the Internet but didn't find any so in the end we agreed to disagree and decided that both orders were accepted in the project.
This may not be a straight answer to the question but I thought I'd share our discussion with others facing the same decision.
Upvotes: 0
Reputation: 20
I think there is no such a coding convention but don't forget about the @Rule annotation.
http://blog.schauderhaft.de/2009/10/04/junit-rules/
Upvotes: 1