Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mark the test method with JUnit 5 annotation.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class CalculatorTest { [1] void testAddition() { int sum = 2 + 3; assertEquals(5, sum); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @Test
Forgetting to add any annotation
Using @Disabled which skips the test
✗ Incorrect
The @Test annotation marks a method as a test method in JUnit 5.
2fill in blank
mediumComplete the code to assert that the result equals 10.
JUnit
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; @Test void testMultiplication() { int result = 2 * 5; [1](10, result); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue with equality expression instead of assertEquals
Using assertNull which checks for null values
✗ Incorrect
assertEquals(expected, actual) checks if the actual value matches the expected value.
3fill in blank
hardFix the error in the test method to make it run correctly.
JUnit
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; @Test void testDivision() { int result = 10 / 2; assertEquals(5, [1]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a literal number instead of the variable 'result'
Swapping expected and actual arguments
✗ Incorrect
The actual value to check is the variable 'result', not a literal.
4fill in blank
hardFill both blanks to create a fast test that runs multiple times.
JUnit
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.RepeatedTest; @RepeatedTest([1]) void testFastAddition() { int sum = 1 + 1; assertEquals([2], sum); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong number of repetitions
Asserting wrong expected sum
✗ Incorrect
RepeatedTest(10) runs the test 10 times; sum of 1 + 1 equals 2.
5fill in blank
hardFill all three blanks to create a test that fails fast on wrong input.
JUnit
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; @Test void testFailFast() { int input = [1]; int expected = [2]; int actual = input * 2; assertEquals([3], actual); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching expected and actual values
Using input that does not match expected output
✗ Incorrect
Input 3 doubled is 6, so expected and actual should be 6 to pass the test.