0
0
JUnittesting~10 mins

Unit testing concept 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 import the JUnit test annotation.

JUnit
import org.junit.[1];
Drag options to blanks, or click blank then click option'
ATest
BBefore
CAfter
DIgnore
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Before or @After instead of @Test for test methods.
Forgetting to import the annotation.
2fill in blank
medium

Complete the code to assert that two values are equal in a test.

JUnit
assertEquals([1], actualValue);
Drag options to blanks, or click blank then click option'
AactualValue
Btrue
Cfalse
DexpectedValue
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping expected and actual values.
Using boolean values instead of expected results.
3fill in blank
hard

Fix the error in the test method declaration by completing the annotation.

JUnit
@[1]
public void testAddition() {
    assertEquals(5, Calculator.add(2, 3));
}
Drag options to blanks, or click blank then click option'
ATest
BBefore
CAfter
DIgnore
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Before or @After instead of @Test for test methods.
Forgetting the annotation causes the test not to run.
4fill in blank
hard

Fill both blanks to create a test method that checks if a list is empty.

JUnit
@[1]
public void testListEmpty() {
    List<String> list = new ArrayList<>();
    assertTrue(list.[2]());
}
Drag options to blanks, or click blank then click option'
ATest
BBefore
CisEmpty
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Before instead of @Test annotation.
Using size() without comparing to zero.
5fill in blank
hard

Fill all three blanks to write a test that checks if a string contains a substring.

JUnit
@[1]
public void testContains() {
    String text = "hello world";
    assertTrue(text.[2]("[3]"));
}
Drag options to blanks, or click blank then click option'
ATest
Bcontains
Cworld
DstartsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using startsWith instead of contains.
Forgetting to annotate the method with @Test.