I have an Integration Test Suite. I have a IntegrationTestBase
class for all my tests to extend. This base class has a @Before
(public void setUp()
) and @After
(public void tearDown()
) method to establish API and DB connections. What I've been doing is just overriding those two methods in each testcase and calling super.setUp()
and super.tearDown()
. However this can cause problems if someone forgets to call the super or puts them at the wrong place and an exception is thrown and they forget to call super in the finally or something.
What I want to do is make the setUp
and tearDown
methods on the base class final
and then just add our own annotated @Before
and @After
methods. Doing some initial tests it appears to always call in this order:
Base @Before
Test @Before
Test
Test @After
Base @After
but I'm just a little concerned that the order isn't guaranteed and that it could cause problems. I looked around and haven't seen anything on the subject. Does anyone know if I can do that and not have any problems?
Code:
public class IntegrationTestBase {
@Before
public final void setUp() { *always called 1st?* }
@After
public final void tearDown() { *always called last?* }
}
public class MyTest extends IntegrationTestBase {
@Before
public final void before() { *always called 2nd?* }
@Test
public void test() { *always called 3rd?* }
@After
public final void after() { *always called 4th?* }
}
I think based on the documentation of the
@Before
and@After
the right conclusion is to give the methods unique names. I use the following pattern in my tests:and
give as a result
Advantage of this approach: Users of the AbstractBaseTest class cannot override the setUp/tearDown methods by accident. If they want to, they need to know the exact name and can do it.
(Minor) disadvantage of this approach: Users cannot see that there are things happening before or after their setUp/tearDown. They need to know that these things are provided by the abstract class. But I assume that's the reason why they use the abstract class
One potential gotcha that has bitten me before:
I like to have at most one
@Before
method in each test class, because order of running the@Before
methods defined within a class is not guaranteed. Typically, I will call such a methodsetUpTest()
.But, although
@Before
is documented asThe @Before methods of superclasses will be run before those of the current class. No other ordering is defined.
, this only applies if each method marked with@Before
has a unique name in the class hierarchy.For example, I had the following:
I expected
AbstractFooTest.setUpTest()
to run beforeFooTest.setUpTest()
, but onlyFooTest.setupTest()
was executed.AbstractFooTest.setUpTest()
was not called at all.The code must be modified as follows to work:
You can use
@BeforeClass
annotation to assure thatsetup()
is always called first. Similarly, you can use@AfterClass
annotation to assure thattearDown()
is always called last.This is usually not recommended, but it is supported.
It's not exactly what you want - but it'll essentially keep your DB connection open the entire time your tests are running, and then close it once and for all at the end.
Yes, this behaviour is guaranteed:
@Before
:@After
:If you turn things around, you can declare your base class abstract, and have descendants declare setUp and tearDown methods (without annotations) that are called in the base class' annotated setUp and tearDown methods.
This isn't an answer to the tagline question, but it is an answer to the problems mentioned in the body of the question. Instead of using @Before or @After, look into using @org.junit.Rule because it gives you more flexibility. ExternalResource (as of 4.7) is the rule you will be most interested in if you are managing connections. Also, If you want guaranteed execution order of your rules use a RuleChain (as of 4.10). I believe all of these were available when this question was asked. Code example below is copied from ExternalResource's javadocs.