Repaso
Español
TestingIntermediateTST-38

Which JUnit 5 annotations run before each test and mark a parameterized test?

Options

  1. @BeforeEach and @ParameterizedTest (Correct option)
  2. @SetupAll and @LoopTest annotations
  3. @InitEach and @ManyTimes annotations
  4. @BeforeTest and @Params annotations

Answer

@BeforeEach and @ParameterizedTest

Why

JUnit 5 (Jupiter) basics:

  • @Test: a test method.
  • @BeforeEach / @AfterEach: run around every test (set up and clean up).
  • @BeforeAll / @AfterAll: once per class (static).
  • @ParameterizedTest with @ValueSource, @CsvSource: run one test with many inputs.
  • @DisplayName, @Disabled, @Nested.

Assertions: assertEquals, assertTrue, assertThrows. AssertJ offers fluent alternatives.

@ParameterizedTest
@CsvSource({"2,3,5", "10,-4,6"})
void adds(int a, int b, int expected) {
    assertEquals(expected, a + b);
}

Remember

@BeforeEach = per test. @ParameterizedTest = many inputs.

Key terms

Practice this question in Repaso