Junior — Middle
What annotations do you use in your tests?
sobes.tech AI
Answer from AI
In Java, annotations from JUnit and sometimes from TestNG are most commonly used for writing tests. The main annotations of JUnit 5:
@Test— marks a method as a test.@BeforeEach— method that runs before each test (initialization).@AfterEach— method that runs after each test (cleanup).@BeforeAll— method that runs once before all tests (static).@AfterAll— method that runs once after all tests (static).@Disabled— disables the test.
Example:
import org.junit.jupiter.api.*;
public class ExampleTest {
@BeforeAll
static void setupAll() {
// Will run once before all tests
}
@BeforeEach
void setup() {
// Will run before each test
}
@Test
void testSomething() {
Assertions.assertTrue(true);
}
@AfterEach
void tearDown() {
// Will run after each test
}
@AfterAll
static void tearDownAll() {
// Will run once after all tests
}
}