import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
}
public class CalculatorTest {
Calculator calculator = new Calculator();
@Test
void testAdd() {
assertEquals(5, calculator.add(2, 3));
}
@Test
void testSubtract() {
assertEquals(1, calculator.subtract(3, 2));
}
// Uncomment this test to increase coverage
/*
@Test
void testMultiply() {
assertEquals(6, calculator.multiply(2, 3));
}
*/
}
/*
Instructions:
1. Run tests with only testAdd and testSubtract active.
2. Generate coverage report using Jacoco or similar.
3. Observe multiply method is not covered.
4. Uncomment testMultiply method.
5. Run tests and generate coverage report again.
6. Observe coverage is now 100%.
*/This code defines a simple Calculator class with three methods: add, subtract, and multiply.
The test class CalculatorTest has tests for add and subtract methods initially. The multiply test is commented out.
When you run tests and generate a coverage report, only add and subtract methods are covered, so coverage is partial.
After uncommenting the multiply test and rerunning, coverage report shows all methods covered, reaching 100% coverage.
This demonstrates how coverage measures test completeness by showing which parts of code are tested.