Complete the code to publish the test result using JUnit's assertion.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class CalculatorTest { @Test public void testAddition() { int result = 2 + 3; assertEquals([1], result); } }
The expected result of 2 + 3 is 5, so assertEquals(5, result) correctly publishes the test result.
Complete the code to publish a test failure when the condition is false.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class NumberTest { @Test public void testIsPositive() { int number = -1; assertTrue([1]); } }
The test expects the number to be positive, so number > 0 is the correct condition to assert true.
Fix the error in the test result publishing code by completing the assertion.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class StringTest { @Test public void testStringLength() { String text = "hello"; assertEquals([1], text.length()); } }
The string "hello" has length 5, so the expected value in assertEquals should be 5.
Fill both blanks to publish test results for checking if a list contains a specific element.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import java.util.List; public class ListTest { @Test public void testContainsElement() { List<String> fruits = List.of("apple", "banana", "cherry"); assertTrue(fruits.[1]([2])); } }
The contains method checks if the list has the element. Here, we check if "banana" is in the list, so fruits.contains("banana") is correct.
Fill all three blanks to publish a test result that verifies a map contains a key with a specific value.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import java.util.Map; public class MapTest { @Test public void testMapEntry() { Map<String, Integer> scores = Map.of("Alice", 90, "Bob", 85); assertEquals([1], scores.get([2])); assertTrue(scores.[3]([2])); } }
The test checks that the score for "Alice" is 90 and that the map contains the key "Alice". So, assertEquals(90, scores.get("Alice")) and assertTrue(scores.containsKey("Alice")) are correct.