Verify correct naming and structure of JUnit test class
Preconditions (2)
✅ Expected Result: The test class CalculatorTest runs successfully and the testAdd method passes, confirming correct naming and structure
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test void testAdd() { Calculator calculator = new Calculator(); int result = calculator.add(2, 3); assertEquals(5, result, "2 + 3 should equal 5"); } } // Simple Calculator class for reference class Calculator { public int add(int a, int b) { return a + b; } }
This test class is named CalculatorTest following the convention of appending 'Test' to the class under test name.
The test method testAdd is annotated with @Test so JUnit knows to run it.
Inside the test method, we create an instance of Calculator and call the add method with inputs 2 and 3.
We use assertEquals to check that the result is 5, which is the expected sum.
This structure and naming make the test clear, focused, and easy to understand.
Now add data-driven testing to verify add method with three different input pairs
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