Complete the code to create a JUnit test method that checks if the sum method returns the correct result.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testSum() { Calculator calc = new Calculator(); int result = calc.sum(2, 3); assertEquals([1], result); } }
The sum of 2 and 3 is 5, so the assertion should check for 5.
Complete the code to add a test method that checks if the isPositive method returns true for positive numbers.
import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; public class NumberTest { @Test public void testIsPositive() { NumberChecker checker = new NumberChecker(); assertTrue(checker.isPositive([1])); } }
The method isPositive should return true for positive numbers, so 5 is correct.
Fix the error in the test method to correctly check if the max method returns the larger number.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class MaxTest { @Test public void testMax() { MaxFinder finder = new MaxFinder(); int max = finder.max(10, 20); assertEquals([1], max); } }
The max method should return the larger number, which is 20 in this case.
Fill both blanks to complete the test method that checks if the isEven method returns true for even numbers and false for odd numbers.
import static org.junit.jupiter.api.Assertions.assert[1]; import static org.junit.jupiter.api.Assertions.assertFalse; import org.junit.jupiter.api.Test; public class EvenOddTest { @Test public void testIsEven() { EvenOddChecker checker = new EvenOddChecker(); assert[2](checker.isEven(4)); assertFalse(checker.isEven(5)); } }
assertTrue is used to check if the method returns true for even numbers.
Fill all three blanks to complete the test method that verifies the behavior of the divide method, including handling division by zero.
import static org.junit.jupiter.api.Assertions.assert[1]; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; public class DivisionTest { @Test public void testDivide() { Calculator calc = new Calculator(); assert[2](2, calc.divide(10, 5)); assertThrows([3].class, () -> calc.divide(10, 0)); } }
assertEquals checks the division result, and assertThrows expects an ArithmeticException for division by zero.