0
0
JUnittesting~10 mins

Failure notification setup 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];

public class SampleTest {
    @Test
    public void testExample() {
        // test code
    }
}
Drag options to blanks, or click blank then click option'
AAfter
BBefore
CTest
DIgnore
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Before or @After instead of @Test
Forgetting to import the annotation
2fill in blank
medium

Complete the code to assert that two values are equal.

JUnit
import static org.junit.Assert.[1];

public class SampleTest {
    @Test
    public void testSum() {
        int result = 2 + 3;
        assertEquals(5, result);
    }
}
Drag options to blanks, or click blank then click option'
AassertTrue
BassertEquals
CassertNull
DassertNotNull
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue with equality instead of assertEquals
Not importing the assertion method
3fill in blank
hard

Fix the error in the test method to notify failure correctly.

JUnit
import static org.junit.Assert.assertEquals;

public class SampleTest {
    @Test
    public void testFailNotification() {
        int expected = 10;
        int actual = 5;
        assertEquals([1], actual);
    }
}
Drag options to blanks, or click blank then click option'
Aexpected
Bactual
C15
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping expected and actual values
Using a wrong literal value
4fill in blank
hard

Fill both blanks to create a test that fails with a custom message.

JUnit
import static org.junit.Assert.assertEquals;

public class SampleTest {
    @Test
    public void testFailWithMessage() {
        assertEquals([1], [2], 100);
    }
}
Drag options to blanks, or click blank then click option'
A"Failure: values differ"
B100
C50
D"Test passed"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the message in the wrong position
Using the actual value instead of expected
5fill in blank
hard

Fill all three blanks to write a test that fails if a condition is false with a message.

JUnit
import static org.junit.Assert.assertTrue;

public class SampleTest {
    @Test
    public void testCondition() {
        assertTrue([1], [2] > [3]);
    }
}
Drag options to blanks, or click blank then click option'
A"Check if x is greater than y"
Bx
Cy
D"Condition met"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping x and y in the condition
Using the wrong message or no message