JUnit5 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is @Timeout used for?

A

@Timeout is used to define a timeout for a test method or a test class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is @RepeatedTest used for?

A

@RepeatedTest is used to repeat a test method a specified number of times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is @BeforeEach used for?

A

@BeforeEach is used to run code before each test method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is @AfterEach used for?

A

@AfterEach is used to run code after each test method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is @BeforeAll used for?

A

@BeforeAll is used to run code once before all test methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is @AfterAll used for?

A

@AfterAll is used to run code once after all test methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is @Disabled used for?

A

@Disabled is used to disable a test method from running

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is @ParameterizedTest used for?

A

@ParameterizedTest is used to signal that a test method has parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

True or False

By default, JUnit creates a new instance of each test class before executing each test method

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or False

Test classes, test methods, and lifecycle methods are not required to be public and cannot be private

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What must be done to be able to declare @BeforeAll and @AfterAll as non-static methods?

A

The test class must be annotated with @TestInstance(Lifecycle.PER_CLASS)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True or False

@Timeout annotations declared at the class level are also applied to lifecycle methods

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Valid or invalid

@ExtendWith({ DatabaseExtension.class, WebServerExtension.class })
class MyFirstTests {
    // ...
}
A

Valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the 4 lifecycle methods?

A
  • @BeforeAll
  • @AfterAll
  • @BeforeEach
  • @AfterEach
17
Q

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

A

True

JUnit Jupiter does not guarantee the execution order of multiple lifecycle methods that are declared within a single test class or test interface

18
Q

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)))
        );
    }
}
A

Valid

19
Q

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

A

True

20
Q

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
    }

}
A

Valid

21
Q

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

A

True

22
Q

How do assumptions work?

A

If the assumption is true, the test will run as usual. If the assumption is false, the test is aborted and marked as ignored