0
0
JUnittesting~10 mins

@BeforeAll method in JUnit - Interactive Code Practice

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

Complete 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'
A@BeforeEach
B@AfterAll
C@Test
D@BeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @BeforeAll
Not making the method static when using @BeforeAll
2fill in blank
medium

Complete 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'
Avoid
Bstatic
Cpublic void
Dprivate void
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting static keyword
Making method non-static without @TestInstance annotation
3fill in blank
hard

Fix 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'
A@BeforeAll static
B@BeforeEach void
C@BeforeAll void
D@AfterAll static void
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll without static
Using @BeforeEach instead of @BeforeAll
4fill in blank
hard

Fill 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'
Apublic
Bstatic
Cvoid
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting static keyword
Using private access modifier
5fill in blank
hard

Fill 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'
Astatic
Bvoid
C"Starting tests"
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Missing public or static keywords
Not using quotes around the print message