The main difference between JUnit and TestNG is the instantiation of the test class. JUnit always creates a new instance of the test class for each test method run. TestNG creates only one instance of the test class, and then runs all the testing methods for this instance.
The JUnit approach guarantees the independence of all testing methods. It just doesn't matter in which order they run. In addition, all instance fields are always the same for each test method. Data initialization, which is common to all testing methods, must take place at the class level, so it must be static. For this reason, the @BeforeClass method must be static.
TestNG approval does not guarantee independence. In fact, you cannot use the instance field in the same way as in JUnit tests. If you change such a field in one test method, the changed value is still observed in another test method. However, this behavior also has an advantage: sometimes there are some dependencies between some testing methods. Using TestNG, the tester can express them.
Due to the single-level approach of TestNG, setting up @BeforeClass can also be a non-stationary method, but it still only executes once. It was a constructive solution, but testers using TestNG should be aware of this.
Seelenvirtuose
source share