nerdexam
Broadcom-VMware

2V0-72.22PSE · Question #73

Which statements is true?

The correct answer is C. Methods annotated with @BeforeAll will run only once before any tests in a class are executed. @BeforeAll marks a method that runs exactly once before any test in the class executes, making it ideal for expensive setup like database connections or file loading - that's what makes C correct. Why the distractors are wrong: A is a swap trick: @AfterEach runs after each…

Testing

Question

Which statements is true?

Options

  • AMethods annotated with @AfterEach will run only once before any tests in a class are executed.
  • B@BeforeAll annotation can only be used on non-static methods.
  • CMethods annotated with @BeforeAll will run only once before any tests in a class are executed.
  • D@AfterEach annotation must be used on a cleanup method when @BeforeEach is used.

How the community answered

(63 responses)
  • A
    3% (2)
  • B
    2% (1)
  • C
    94% (59)
  • D
    2% (1)

Explanation

@BeforeAll marks a method that runs exactly once before any test in the class executes, making it ideal for expensive setup like database connections or file loading - that's what makes C correct.

Why the distractors are wrong:

  • A is a swap trick: @AfterEach runs after each individual test, not once before all tests - that describes @BeforeAll's behavior with the wrong annotation.
  • B is the opposite of reality: @BeforeAll methods must be static (unless the test class uses @TestInstance(Lifecycle.PER_CLASS)), not non-static.
  • D is a made-up rule: @BeforeEach and @AfterEach are independent - you can use either without the other depending on your needs.

Memory tip: Think in pairs and timing - Before/After = when it runs, All/Each = how many times. @BeforeAll = "before ALL tests, just once"; @AfterEach = "after EACH test, every time."

Topics

#JUnit 5#Test Lifecycle#@BeforeAll#Annotations

Community Discussion

No community discussion yet for this question.

Full 2V0-72.22PSE Practice