Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mark the method to run once before all tests.
JUnit
public class CalculatorTest { [1] static void setup() { System.out.println("Setup before all tests"); } @Test void testAdd() { // test code here } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @BeforeAll
Not making the method static when using @BeforeAll
✗ Incorrect
The @BeforeAll annotation marks a method to run once before all tests in the class.
2fill in blank
mediumComplete the code to make the @BeforeAll method valid in JUnit 5.
JUnit
public class DatabaseTest { @BeforeAll [1] void init() { System.out.println("Initialize DB connection"); } @Test void testQuery() { // test code } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting static keyword
Making method non-static without @TestInstance annotation
✗ Incorrect
In JUnit 5, @BeforeAll methods must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).
3fill in blank
hardFix the error in the @BeforeAll method declaration.
JUnit
public class ServiceTest { [1] void setup() { System.out.println("Setup service"); } @Test void testService() { // test code } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll without static
Using @BeforeEach instead of @BeforeAll
✗ Incorrect
The @BeforeAll method must be static and annotated properly to run once before all tests.
4fill in blank
hardFill both blanks to correctly declare a @BeforeAll method with public access.
JUnit
public class ApiTest { @BeforeAll [1] [2] void setupAll() { System.out.println("API setup"); } @Test void testApi() { // test code } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting static keyword
Using private access modifier
✗ Incorrect
The method must be public and static to be a valid @BeforeAll method.
5fill in blank
hardFill all three blanks to create a valid @BeforeAll method that prints a message.
JUnit
public class UserTest { @BeforeAll [1] [2] void setup() { System.out.println([3]); } @Test void testUser() { // test code } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing public or static keywords
Not using quotes around the print message
✗ Incorrect
The method must be public static void and print the message string.