@BeforeClass: Difference between revisions
Jump to navigation
Jump to search
Line 12: | Line 12: | ||
TestBase.java | TestBase.java | ||
│ @BeforeClass | │ @BeforeClass | ||
│ public static void | │ public static void testBaseOneTimeSetup() { | ||
│ ... | │ ... | ||
│ } | │ } | ||
Line 18: | Line 18: | ||
└── SomeTest.java | └── SomeTest.java | ||
@BeforeClass | @BeforeClass | ||
public static void | public static void someTestOneTimeSetup() { | ||
... | ... | ||
} | } | ||
</font> | </font> | ||
then both methods are executed only once, in this order: first the parent class method (<code> | then both methods are executed only once, in this order: first the parent class method (<code>testBaseOneTimeSetup()</code>), then the subclass method (<code>someTestOneTimeSetup()</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: | However, if both methods '''have the same name''', an override of sorts takes place and only the method belonging to the subclass is executed: | ||
Line 28: | Line 28: | ||
TestBase.java | TestBase.java | ||
│ @BeforeClass | │ @BeforeClass | ||
│ public static void | │ public static void oneTimeClassSetup() { | ||
│ ... | │ ... | ||
│ } | │ } | ||
Line 34: | Line 34: | ||
└── SomeTest.java | └── SomeTest.java | ||
@BeforeClass | @BeforeClass | ||
public static void | public static void oneTimeClassSetup() { | ||
... | ... | ||
} | } | ||
</font> | </font> | ||
Only <code>SomeTest. | Only <code>SomeTest.oneTimeClassSetup()</code> is executed, so if you need the logic executed in superclass, it must be invoked explicitly with: | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
class SomeTest { | class SomeTest { | ||
@BeforeClass | @BeforeClass | ||
public static void | public static void oneTimeClassSetup() { | ||
TestBase. | TestBase.oneTimeClassSetup(); | ||
} | } | ||
} | } | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> |
Revision as of 01:55, 29 August 2021
Internal
Overview
The annotation must be attached to a static method.
If two methods with different names are annotated with @BeforeClass
in a test class hierarchy, as shown below:
TestBase.java │ @BeforeClass │ public static void testBaseOneTimeSetup() { │ ... │ } │ └── SomeTest.java @BeforeClass public static void someTestOneTimeSetup() { ... }
then both methods are executed only once, in this order: first the parent class method (testBaseOneTimeSetup()
), then the subclass method (someTestOneTimeSetup()
). 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 │ @BeforeClass │ public static void oneTimeClassSetup() { │ ... │ } │ └── SomeTest.java @BeforeClass public static void oneTimeClassSetup() { ... }
Only SomeTest.oneTimeClassSetup()
is executed, so if you need the logic executed in superclass, it must be invoked explicitly with:
<syntaxhighlight lang='java'>
class SomeTest {
@BeforeClass public static void oneTimeClassSetup() { TestBase.oneTimeClassSetup(); }
} <syntaxhighlight lang='java'>