Backend Low Level Design 4
About Lesson

Test Annotations are used in Java test files to specify which methods are test methods. These annotations help the testing framework identify which parts of the code should be executed as tests. Here are some commonly used test annotations in Java:

  1. @Test: The @Test annotation marks a method as a test method, causing it to run automatically without needing to be called explicitly. The testing framework will execute all methods marked with this annotation. When this annotation is used, a green play button appears to run the test separately. This button only appears when either a public void main method is present or when the method is annotated with @Test. It doesn’t appear for normal methods.

  2. @Before: This annotation is used to mark a method that should be run before each test method in the class. It’s often used for setup tasks, like initializing objects or resources needed for the tests.

  3. @After: This annotation is used to mark a method that should be run after each test method in the class. It’s often used for cleanup tasks, like releasing resources or resetting the state.

  4. @BeforeClass: This annotation is used to mark a method that should be run once before any of the test methods in the class are executed. It’s often used for setup tasks that only need to be done once.

  5. @AfterClass: This annotation is used to mark a method that should be run once after all the test methods in the class have been executed. It’s often used for cleanup tasks that only need to be done once.

© GeekySanjay