Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to run a simple JUnit test method.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { @Test public void testAddition() { int result = 2 + 3; assertEquals([1], result); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong expected value in assertEquals.
Confusing actual and expected parameters.
✗ Incorrect
The sum of 2 and 3 is 5, so assertEquals should check for 5.
2fill in blank
mediumComplete the code to annotate a method as a JUnit test.
JUnit
import org.junit.jupiter.api.Test; public class SampleTest { [1] public void sampleMethod() { // test code here } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using setup or teardown annotations instead of @Test.
Forgetting to annotate the test method.
✗ Incorrect
The @Test annotation marks a method as a test method in JUnit.
3fill in blank
hardFix the error in the assertion to correctly check string equality.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class StringTest { @Test public void testGreeting() { String greeting = "Hello"; assertEquals([1], greeting); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different case in the expected string.
Passing null or unrelated strings.
✗ Incorrect
The expected value must exactly match the actual string "Hello" for the test to pass.
4fill in blank
hardFill both blanks to create a test that fails if the number is not positive.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class NumberTest { @Test public void testPositive() { int number = -5; assertTrue(number [1] [2]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than.
Comparing to -1 instead of 0.
✗ Incorrect
The test checks that number is greater than 0 to ensure it is positive.
5fill in blank
hardFill all three blanks to create a test that checks if a list contains a specific element.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; public class ListTest { @Test public void testContains() { List<String> fruits = List.of("apple", "banana", "cherry"); assertTrue(fruits.[1]([2]), "List should contain [3]."); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the element without quotes causing a compile error.
Using the wrong method name.
Mismatch between the element checked and the message.
✗ Incorrect
The method 'contains' checks if the list has the string "banana". The message also uses "banana".