Test Overview
This test class checks if the Calculator's add method correctly adds two numbers. It verifies that the sum is as expected.
This test class checks if the Calculator's add method correctly adds two numbers. It verifies that the sum is as expected.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testAdd() { Calculator calc = new Calculator(); int result = calc.add(2, 3); assertEquals(5, result, "2 + 3 should equal 5"); } } class Calculator { public int add(int a, int b) { return a + b; } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and loads CalculatorTest class | JUnit test environment initialized with CalculatorTest loaded | — | PASS |
| 2 | JUnit invokes testAdd method | Inside testAdd method, Calculator instance created | — | PASS |
| 3 | Calls Calculator.add(2, 3) | Calculator.add method executes and returns 5 | — | PASS |
| 4 | Assert that result equals 5 | Result is 5, expected is 5 | assertEquals(5, result, "2 + 3 should equal 5") | PASS |
| 5 | Test method completes successfully | Test runner marks testAdd as passed | — | PASS |
Test to the class name being tested.Test at the end of the class name follows the standard convention by using the suffix Test. Options B, C, and D do not follow common or recommended practices.Test at the end of the class name -> Option Cclass and the class name should follow Java naming conventions (PascalCase). The class should be public if accessed externally.Test. public test class Calculator { } uses an invalid keyword test. public class calculator_test { } uses underscores and lowercase which is against Java class naming conventions.public class MathUtilsTest {
@org.junit.jupiter.api.Test
void testAdd() {
int result = 2 + 3;
org.junit.jupiter.api.Assertions.assertEquals(5, result);
}
@org.junit.jupiter.api.Test
void testSubtract() {
int result = 5 - 3;
org.junit.jupiter.api.Assertions.assertEquals(1, result);
}
}public class StringUtilsTest {
@Test
public void testIsEmpty() {
boolean result = StringUtils.isEmpty("");
assertTrue(result);
}
}@Test annotation and assertTrue method but does not show imports. Without importing org.junit.jupiter.api.Test and org.junit.jupiter.api.Assertions.assertTrue, this code will not compile.assertTrue is a valid assertion method. Class name ending with 'Test' is correct.OrderProcessor. Which of the following is the best name for its JUnit test class and a test method that checks if an order is processed successfully?Test suffix to the class under test. Class: OrderProcessorTest, Method: testProcessOrderSuccess uses OrderProcessorTest, which is correct. Class: TestOrderProcessor, Method: processOrder prefixes with 'Test', which is less common. Class: OrderProcessorTests, Method: checkOrder uses plural 'Tests', which is acceptable but less standard. Class: OrderProcessor_Test, Method: orderSuccessTest uses underscore, which is not recommended.OrderProcessorTest, Method: testProcessOrderSuccess's testProcessOrderSuccess clearly states the test purpose. Other options are less descriptive or ambiguous.OrderProcessorTest, Method: testProcessOrderSuccess [OK]Pass the test and this pattern moves unknown