import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.io.File;
public class CalculatorTest {
@Test
public void testAdd() {
Calculator calc = new Calculator();
assertEquals(5, calc.add(2, 3));
assertEquals(-1, calc.add(2, -3));
}
@Test
public void testSubtract() {
Calculator calc = new Calculator();
assertEquals(1, calc.subtract(3, 2));
assertEquals(5, calc.subtract(2, -3));
}
@Test
public void testCoverageReportExists() {
// Assuming Maven default target directory and JaCoCo report path
File report = new File("target/site/jacoco/index.html");
assertTrue(report.exists(), "Coverage report should exist after tests run");
}
}
// Sample Calculator class
class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
}
/*
Note: To generate coverage report, run:
mvn clean test jacoco:report
Then open target/site/jacoco/index.html to verify coverage manually.
*/The test class CalculatorTest contains three tests:
- testAdd() verifies the
add method with two cases. - testSubtract() verifies the
subtract method with two cases. - testCoverageReportExists() checks if the JaCoCo coverage report file exists after running tests.
The Calculator class is simple with two methods to test.
Running mvn clean test jacoco:report will execute tests and generate the coverage report.
The coverage report file existence is asserted programmatically to confirm report generation.
Manual verification of coverage percentage is recommended by opening the HTML report in a browser.