Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@BeforeClass and inheritance - order of execution

I have an abstract base class, which I use as a base for my unit tests (TestNG 5.10). In this class, I initialize the whole environment for my tests, setting up database mappings, etc. This abstract class has a method with a @BeforeClass annotation which does the initialization.

Next, I extend that class with specific classes in which I have @Test methods and also @BeforeClass methods. These methods do class-specific initialization of the environment (e.g. put some records into the database).

How I can enforce a specific order of the @BeforeClass annotated methods? I need the ones from the abstract base class to be executed before the ones of the extending class.

Example:

abstract class A {     @BeforeClass     doInitialization() {...} }  class B extends A {     @BeforeClass     doSpecificInitialization() {...}      @Test     doTests() {...} } 

Expected order:

A.doInitialization B.doSpecificInitialization B.doTests 

Actual order:

B.doSpecificInitialization // <- crashes, as the base init is missing (A.doInitialization        // <---not executed  B.doTests)                // <-/ 
like image 246
Dominik Sandjaja Avatar asked Jan 25 '10 14:01

Dominik Sandjaja


People also ask

What is the difference between @BeforeClass and @before?

@BeforeClass will be run before the entire test suits whereas @Before will be run is executed before each test, while @BeforeClass runs once before the entire test fixture. If your test class has ten tests, @Before code will be executed ten times, but @BeforeClass will be executed only once.

What is the difference between @before and @BeforeClass annotation?

The Before annotation indicates that this method must be executed before each test in the class, so as to execute some preconditions necessary for the test. The BeforeClass annotation indicates that the static method to which is attached must be executed once and before all tests in the class.

What is @BeforeClass in TestNG?

@BeforeClass annotated method will be run before the first test method in the current class is invoked. The following is a list of attributes supported by the @BeforeClass annotation: Attribute. Description.

Which of the following annotation is used for actions to be performed before each test class?

The @BeforeMethod annotated method will be executed before each test method will run. The @AfterMethod annotated method will run after the execution of each test method.


1 Answers

edit: Answer below is for JUnit, but I will leave it here anyway, because it could be helpful.

According to the JUnit api: "The @BeforeClass methods of superclasses will be run before those the current class."

I tested this, and it seems to work for me.

However, as @Odys mentions below, for JUnit you need to have the two methods named differently though as doing otherwise will result in only the subclass method being run because the parent will be shadowed.

like image 146
Fortega Avatar answered Oct 13 '22 14:10

Fortega