0
0
JUnittesting~10 mins

Why extensions customize JUnit behavior - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to register a JUnit extension using the correct annotation.

JUnit
@[1] MyExtension extension = new MyExtension();
Drag options to blanks, or click blank then click option'
ARegisterExtension
BExtendWith
CTestExtension
DEnableExtension
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ExtendWith instead of @RegisterExtension for instance fields.
Misspelling the annotation name.
2fill in blank
medium

Complete the code to implement the JUnit extension interface for before each test callback.

JUnit
public class MyExtension implements [1] { }
Drag options to blanks, or click blank then click option'
AAfterTestExecutionCallback
BBeforeAllCallback
CTestExecutionExceptionHandler
DBeforeEachCallback
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing AfterTestExecutionCallback which runs after tests.
Confusing BeforeAllCallback which runs once before all tests.
3fill in blank
hard

Fix the error in the extension method signature to correctly override the beforeEach method.

JUnit
@Override
public void beforeEach([1] context) throws Exception { }
Drag options to blanks, or click blank then click option'
AExecutionContext
BExtensionContext
CTestContext
DTestInfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using TestContext which is not part of JUnit 5 extension API.
Using TestInfo which is for test methods, not extension callbacks.
4fill in blank
hard

Fill both blanks to implement an extension that prints a message before and after each test.

JUnit
public class MyExtension implements [1], [2] {
  @Override
  public void beforeEach(ExtensionContext context) {
    System.out.println("Starting test");
  }
  @Override
  public void afterEach(ExtensionContext context) {
    System.out.println("Finished test");
  }
}
Drag options to blanks, or click blank then click option'
ABeforeEachCallback
BAfterAllCallback
CAfterEachCallback
DBeforeAllCallback
Attempts:
3 left
💡 Hint
Common Mistakes
Using AfterAllCallback or BeforeAllCallback which run once per test class, not per test method.
5fill in blank
hard

Fill all three blanks to create a dynamic test factory method using JUnit extension.

JUnit
@TestFactory
Stream<DynamicTest> dynamicTests() {
  return Stream.of("A", "B", "C")
    .map([1] -> DynamicTest.dynamicTest(
      "Test " + [2],
      () -> assertTrue([3].length() > 0)
    ));
}
Drag options to blanks, or click blank then click option'
Aname
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently inside the lambda.
Using 'name' which is not declared as the lambda parameter.