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…
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)- A3% (2)
- B2% (1)
- C94% (59)
- D2% (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:
@AfterEachruns after each individual test, not once before all tests - that describes@BeforeAll's behavior with the wrong annotation. - B is the opposite of reality:
@BeforeAllmethods must bestatic(unless the test class uses@TestInstance(Lifecycle.PER_CLASS)), not non-static. - D is a made-up rule:
@BeforeEachand@AfterEachare 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
Community Discussion
No community discussion yet for this question.