A learning project demonstrating unit testing with JUnit 5, focusing on statement coverage and path coverage testing.
.
├── src/
│ ├── main/java/com/example/
│ │ └── Calculator.java
│ └── test/java/com/example/
│ ├── CalculatorStatementCoverageTest.java
│ └── CalculatorPathCoverageTest.java
├── pom.xml
├── ISSUE_1.md
├── ISSUE_2.md
└── README.md
The Calculator class contains three main methods with loops and branching logic:
- Calculates the sum of numbers from 1 to n
- Even numbers are doubled before adding
- Throws
IllegalArgumentExceptionif n is negative - Loops:
forloop iterating from 1 to n - Branching:
if-elsechecking even/odd
- Categorizes a number as positive/negative, even/odd, or zero
- Returns a descriptive string
- Branching: Multiple nested
if-elsestatements
- Counts how many times a target number appears in an array
- Stops counting if count exceeds 100
- Loops:
for-eachloop through array - Branching:
ifstatements for null check, matching, and break condition
CalculatorStatementCoverageTest.java ensures every line of code is executed:
- Tests cover all methods and all branches
- Includes edge cases and exception handling
- Verifies correctness of each statement
Test Methods:
testCalculateSumWithZero()testCalculateSumWithOne()testCalculateSumWithTwo()testCalculateSumWithFive()testCalculateSumWithNegative()testCategorizePositiveOdd()testCategorizePositiveEven()testCategorizeNegativeOdd()testCategorizeNegativeEven()testCategorizeZero()testCountOccurrencesWithNull()testCountOccurrencesWithEmpty()testCountOccurrencesNoMatch()testCountOccurrencesSingleMatch()testCountOccurrencesMultipleMatches()testCountOccurrencesBreakCondition()
CalculatorPathCoverageTest.java ensures all execution paths are tested:
- Tests all decision branches
- Covers nested conditions
- Tests boundary values
Path Coverage by Method:
calculateSum(): 5 pathscategorizeNumber(): 5 pathscountOccurrences(): 5 paths- Plus comprehensive and boundary tests
mvn clean testRun the test classes directly from your IDE (IntelliJ IDEA, Eclipse, VS Code).
# Compile
javac -d target/classes src/main/java/com/example/Calculator.java
# Compile tests (requires JUnit 5 in classpath)
javac -cp target/classes:junit-jupiter-api-5.9.3.jar:junit-jupiter-engine-5.9.3.jar \
-d target/test-classes \
src/test/java/com/example/CalculatorStatementCoverageTest.javaTo push this repository to GitHub:
- Go to https://github.com/new
- Name your repository (e.g.,
calculator-testing-lab) - Choose public or private
- DO NOT initialize with README (we have one)
git remote add origin https://github.com/YOUR_USERNAME/calculator-testing-lab.git
git branch -M main
git push -u origin main- Check that all commits are visible
- Issues #1 and #2 are documented in ISSUE_1.md and ISSUE_2.md
This lab demonstrates:
- ✅ Writing code with loops and conditional branching
- ✅ Creating comprehensive unit tests with JUnit 5
- ✅ Understanding statement coverage (C1 coverage)
- ✅ Understanding path coverage (decision coverage)
- ✅ Using Git for version control
- ✅ Following TDD and testing best practices
The test suite achieves:
- Statement Coverage: 100% (every statement executed)
- Branch Coverage: 100% (every decision taken both ways)
- Path Coverage: 100% (all execution paths tested)
- Java 11+
- JUnit Jupiter (JUnit 5) 5.9.3
- Maven 3.6+ (optional, for building)