Complete the code to create a JUnit test method that checks if a number is positive.
import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; public class NumberTest { @Test public void testPositive() { int number = 5; assertTrue(number [1] 0); } }
The assertion checks if the number is greater than zero, meaning it is positive.
Complete the code to assert that a string is not null in a JUnit test.
import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Test; public class StringTest { @Test public void testStringNotNull() { String text = "hello"; assertNotNull([1]); } }
The assertion checks that the variable 'text' is not null.
Fix the error in the JUnit test assertion that checks equality of two integers.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class EqualityTest { @Test public void testEqualNumbers() { int expected = 10; int actual = 5 + 5; assertEquals([1], actual); } }
The first argument to assertEquals should be the expected value, which is 'expected'.
Fill both blanks to complete a JUnit test that checks if a list contains a specific element.
import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; import org.junit.jupiter.api.Test; public class ListTest { @Test public void testListContains() { List<String> fruits = List.of("apple", "banana", "cherry"); assertTrue(fruits.[1]("banana") [2] true); } }
The method 'contains' checks if the list has the element. The '==' operator compares the result to true.
Fill all three blanks to complete a JUnit test that verifies a map contains a key and its value is correct.
import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Map; import org.junit.jupiter.api.Test; public class MapTest { @Test public void testMapKeyValue() { Map<String, Integer> ages = Map.of("Alice", 30, "Bob", 25); assertEquals([1], ages.get([2])); assertEquals([3], ages.size()); } }
The test checks that the value for key "Alice" is 30 and the map size is 2.