@AfterClass: Difference between revisions
(Created page with "=Internal= * JUnit =Overview= The annotation must be attached to a '''static''' method.") |
|||
(One intermediate revision by the same user not shown) | |||
Line 2: | Line 2: | ||
* [[JUnit#Annotations|JUnit]] | * [[JUnit#Annotations|JUnit]] | ||
* [[@BeforeClass]] | |||
=Overview= | =Overview= | ||
The annotation must be attached to a '''static''' method. | The annotation must be attached to a '''static''' method. | ||
=<tt>@AfterClass</tt> and Class Hierarchies= | |||
If two methods '''with different names''' are annotated with <code>@AfterClass</code> in a test class hierarchy, as shown below: | |||
<font size='-1'> | |||
TestBase.java | |||
│ @AfterClass | |||
│ public static void testBaseOneTimeTeardown() { | |||
│ ... | |||
│ } | |||
│ | |||
└── SomeTest.java | |||
@AfterClass | |||
public static void someTestOneTimeTeardown() { | |||
... | |||
} | |||
</font> | |||
then both methods are executed only once, in this order: first the subclass method (<code>someTestOneTimeTeardown()</code>), then the parent class method (<code>testBaseOneTimeTeardown()</code>). The rule holds true in a multi-layer hierarchy. | |||
However, if both methods '''have the same name''', an override of sorts takes place and only the method belonging to the subclass is executed: | |||
<font size='-1'> | |||
TestBase.java | |||
│ @AfterClass | |||
│ public static void oneTimeClassTeardown() { | |||
│ ... | |||
│ } | |||
│ | |||
└── SomeTest.java | |||
@AfterClass | |||
public static void oneTimeClassTeardown() { | |||
... | |||
} | |||
</font> | |||
Only <code>SomeTest.oneTimeClassTeardown()</code> (the most specific subclass) is executed, so if you need the logic executed in superclass, it must be invoked explicitly with: | |||
<syntaxhighlight lang='java'> | |||
class SomeTest { | |||
@AfterClass | |||
public static void oneTimeClassTeardown() { | |||
TestBase.oneTimeClassTeardown(); | |||
} | |||
} | |||
</syntaxhighlight> | |||
In conclusion, if class-level one-time cleanup is needed for each level of a class hierarchy, the best solution is to name <code>@AfterClass</code> methods differently for each class. This way, all methods are executed in order, starting from the bottom of the hierarchy (the most specific subclass) and continuing in order towards the root of the hierarchy. | |||
Recommended naming convention: | |||
<syntaxhighlight lang='java'> | |||
class SomeTest { | |||
@AfterClass | |||
public static void someTestOneTimeTeardown() { | |||
... | |||
} | |||
} | |||
</syntaxhighlight> |
Latest revision as of 02:14, 29 August 2021
Internal
Overview
The annotation must be attached to a static method.
@AfterClass and Class Hierarchies
If two methods with different names are annotated with @AfterClass
in a test class hierarchy, as shown below:
TestBase.java │ @AfterClass │ public static void testBaseOneTimeTeardown() { │ ... │ } │ └── SomeTest.java @AfterClass public static void someTestOneTimeTeardown() { ... }
then both methods are executed only once, in this order: first the subclass method (someTestOneTimeTeardown()
), then the parent class method (testBaseOneTimeTeardown()
). The rule holds true in a multi-layer hierarchy.
However, if both methods have the same name, an override of sorts takes place and only the method belonging to the subclass is executed:
TestBase.java │ @AfterClass │ public static void oneTimeClassTeardown() { │ ... │ } │ └── SomeTest.java @AfterClass public static void oneTimeClassTeardown() { ... }
Only SomeTest.oneTimeClassTeardown()
(the most specific subclass) is executed, so if you need the logic executed in superclass, it must be invoked explicitly with:
class SomeTest {
@AfterClass
public static void oneTimeClassTeardown() {
TestBase.oneTimeClassTeardown();
}
}
In conclusion, if class-level one-time cleanup is needed for each level of a class hierarchy, the best solution is to name @AfterClass
methods differently for each class. This way, all methods are executed in order, starting from the bottom of the hierarchy (the most specific subclass) and continuing in order towards the root of the hierarchy.
Recommended naming convention:
class SomeTest {
@AfterClass
public static void someTestOneTimeTeardown() {
...
}
}