JUnit5 Flashcards
What is @Timeout
used for?
@Timeout
is used to define a timeout for a test method or a test class
What is @RepeatedTest
used for?
@RepeatedTest
is used to repeat a test method a specified number of times
What is @BeforeEach
used for?
@BeforeEach
is used to run code before each test method
What is @AfterEach
used for?
@AfterEach
is used to run code after each test method
What is @BeforeAll
used for?
@BeforeAll
is used to run code once before all test methods
What is @AfterAll
used for?
@AfterAll
is used to run code once after all test methods
What is @Disabled
used for?
@Disabled
is used to disable a test method from running
What is @ParameterizedTest
used for?
@ParameterizedTest
is used to signal that a test method has parameters
True or False
Although true unit tests typically should not rely on the order in which they are executed, there are times when it is necessary to enforce a specific test method execution order
True
True or False
By default, JUnit creates a new instance of each test class before executing each test method
True
True or False
Test classes, test methods, and lifecycle methods are not required to be public and cannot be private
True
What must be done to be able to declare @BeforeAll and @AfterAll as non-static methods?
The test class must be annotated with @TestInstance(Lifecycle.PER_CLASS)
True or False
@Timeout annotations declared at the class level are also applied to lifecycle methods
False
True or False
@ExtendWith may also be declared on fields or on parameters in test class constructors, in test methods, and in @BeforeAll, @AfterAll, @BeforeEach, and @AfterEach lifecycle methods
True
Valid or invalid
@ExtendWith({ DatabaseExtension.class, WebServerExtension.class }) class MyFirstTests { // ... }
Valid
What are the 4 lifecycle methods?
@BeforeAll
@AfterAll
@BeforeEach
@AfterEach
True or False
The JUnit Team recommends that developers declare at most one @BeforeEach method and at most one @AfterEach method per test class or test interface unless there are no dependencies between the @BeforeEach methods or between the @AfterEach methods
True
JUnit Jupiter does not guarantee the execution order of multiple lifecycle methods that are declared within a single test class or test interface
Valid or Invalid
@ParameterizedTest(name = "{index} - {0} is older than 40") @ArgumentsSource(EmployeesArgumentsProvider.class) void isEmployeeAgeGreaterThan40(Employee e) { assertTrue(Period.between(e.getDob(), LocalDate.now()).get(ChronoUnit.YEARS) > 40); } class EmployeesArgumentsProvider implements ArgumentsProvider { @Override public Stream<? extends Arguments> provideArguments(ExtensionContext context) { return Stream.of( Arguments.of(new Employee(1, "Alex", LocalDate.of(1980, 2, 3))), Arguments.of(new Employee(2, "Brian", LocalDate.of(1979, 2, 3))), Arguments.of(new Employee(3, "Charles", LocalDate.of(1978, 2, 3))) ); } }
Valid
True or False
@ParameterizedTest methods must specify at least one ArgumentsProvider via @ArgumentsSource or a corresponding composed annotation (e.g., @ValueSource, @CsvSource, etc.). The provider is responsible for providing a Stream of Arguments that will be used to invoke the parameterized test method
True
Valid or Invalid
@TestMethodOrder(OrderAnnotation.class) class OrderedTestsDemo { @Test @Order(1) void nullValues() { // perform assertions against null values } @Test @Order(2) void emptyValues() { // perform assertions against empty values } @Test @Order(3) void validValues() { // perform assertions against valid values } }
Valid
True or False
It is generally recommended to omit the public modifier for test classes, test methods, and lifecycle methods unless there is a technical reason for doing so
True
How do assumptions work?
If the assumption is true, the test will run as usual. If the assumption is false, the test is aborted and marked as ignored