All contents of the article are extracted from the book JUnit In Action, 2nd edition by Petar Tahchiev etc.
The requirements to define a test class
are that the class must be public and contain a zero-argument constructor.
The requirements to create a test method
are that it must be annotated with@Test, be public, take no arguments, and return void.
Independecy of Each Test Method
JUnit creates a new instance of the test class before invoking each @Test method.This helps provide independence between test methods and avoids unintentional side effects in the test code. Because each test method runs on a new test class instance,we can’t reuse instance variable values across test methods
@Before and @After Annotation
The @Before and @After annotated methods are executed right before/after the execution of each one of your @Test methods and regardless of whether the test
failed or not.
You can have as many of these methods as you want, but beware that if you have more than one of the @Before/@After methods, the order of their execution is not defined.
@BeforeClass and @AfterClass annotations
the @BeforeClass and @AfterClass annotations to annotate your methods in that class. The methods that you annotate will get executed, only once, before/after all of your @Test methods. you can have as many of these methods as you want, and again the order of the execution is unspecified.
the @Before/@After annotated methods must be public. The @BeforeClass/@AfterClass annotated methods must be public and static.