Skip to content

baamvu/calculator-testing-lab1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calculator Application - Testing Lab

A learning project demonstrating unit testing with JUnit 5, focusing on statement coverage and path coverage testing.

Project Structure

.
├── 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

Calculator Class

The Calculator class contains three main methods with loops and branching logic:

1. calculateSum(int n)

  • Calculates the sum of numbers from 1 to n
  • Even numbers are doubled before adding
  • Throws IllegalArgumentException if n is negative
  • Loops: for loop iterating from 1 to n
  • Branching: if-else checking even/odd

2. categorizeNumber(int num)

  • Categorizes a number as positive/negative, even/odd, or zero
  • Returns a descriptive string
  • Branching: Multiple nested if-else statements

3. countOccurrences(int[] arr, int target)

  • Counts how many times a target number appears in an array
  • Stops counting if count exceeds 100
  • Loops: for-each loop through array
  • Branching: if statements for null check, matching, and break condition

Test Cases

Issue #1: Statement Coverage (16 tests)

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()

Issue #2: Path Coverage (27 tests)

CalculatorPathCoverageTest.java ensures all execution paths are tested:

  • Tests all decision branches
  • Covers nested conditions
  • Tests boundary values

Path Coverage by Method:

  • calculateSum(): 5 paths
  • categorizeNumber(): 5 paths
  • countOccurrences(): 5 paths
  • Plus comprehensive and boundary tests

Running Tests

With Maven:

mvn clean test

With IDE:

Run the test classes directly from your IDE (IntelliJ IDEA, Eclipse, VS Code).

With Java Compiler:

# 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.java

Pushing to GitHub

To push this repository to GitHub:

1. Create a new repository on 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)

2. Connect local repo to remote

git remote add origin https://github.com/YOUR_USERNAME/calculator-testing-lab.git
git branch -M main
git push -u origin main

3. Verify on GitHub

  • Check that all commits are visible
  • Issues #1 and #2 are documented in ISSUE_1.md and ISSUE_2.md

Learning Objectives

This lab demonstrates:

  1. ✅ Writing code with loops and conditional branching
  2. ✅ Creating comprehensive unit tests with JUnit 5
  3. ✅ Understanding statement coverage (C1 coverage)
  4. ✅ Understanding path coverage (decision coverage)
  5. ✅ Using Git for version control
  6. ✅ Following TDD and testing best practices

Coverage Metrics

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)

Dependencies

  • Java 11+
  • JUnit Jupiter (JUnit 5) 5.9.3
  • Maven 3.6+ (optional, for building)

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages