improve and enhance test coverage #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: 'Java SDK - Unit Testing' | |
| # This workflow runs ONLY unit tests (excludes integration tests ending with IT.java) | |
| # Integration tests require network access and valid .env credentials | |
| on: | |
| pull_request: | |
| branches: | |
| - development | |
| - staging | |
| - main | |
| jobs: | |
| coverage: | |
| name: Unit Test Coverage Check | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| checks: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up JDK 8 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '8' | |
| distribution: 'temurin' | |
| cache: 'maven' | |
| - name: Run unit tests (excluding integration tests) | |
| run: | | |
| echo "Running unit tests only (excluding *IT.java files)..." | |
| mvn clean test -Dtest='!*IT' -Dgpg.skip=true | |
| continue-on-error: false | |
| - name: Generate JaCoCo coverage report | |
| run: | | |
| echo "Generating JaCoCo report..." | |
| mvn jacoco:report -Dgpg.skip=true | |
| echo "✅ Coverage report generated" | |
| if: always() | |
| - name: Verify JaCoCo reports generated | |
| run: | | |
| echo "Checking for JaCoCo reports..." | |
| echo "Current directory: $(pwd)" | |
| echo "Target directory contents:" | |
| ls -la target/ || echo "No target directory found" | |
| if [ -d "target/site/jacoco" ]; then | |
| echo "JaCoCo directory contents:" | |
| ls -lh target/site/jacoco/ | |
| fi | |
| if [ ! -f "target/site/jacoco/jacoco.xml" ]; then | |
| echo "❌ Error: jacoco.xml not found" | |
| echo "This usually means tests didn't run or JaCoCo plugin isn't configured correctly" | |
| exit 1 | |
| fi | |
| if [ ! -f "target/site/jacoco/jacoco.csv" ]; then | |
| echo "⚠️ Warning: jacoco.csv not found (badge generation will be skipped)" | |
| fi | |
| echo "✅ JaCoCo XML report found" | |
| if: always() | |
| - name: Generate JaCoCo Badge | |
| id: jacoco | |
| uses: cicirello/jacoco-badge-generator@v2 | |
| if: hashFiles('target/site/jacoco/jacoco.csv') != '' | |
| continue-on-error: true | |
| with: | |
| jacoco-csv-file: target/site/jacoco/jacoco.csv | |
| badges-directory: .github/badges | |
| generate-branches-badge: true | |
| generate-summary: true | |
| - name: Check coverage thresholds | |
| id: coverage-check | |
| if: hashFiles('target/site/jacoco/jacoco.xml') != '' | |
| run: | | |
| echo "Checking coverage thresholds (unit tests only)..." | |
| # Extract coverage percentages from JaCoCo XML report (using sed for cross-platform compatibility) | |
| INSTRUCTION_COVERAGE=$(sed -n 's/.*type="INSTRUCTION".*covered="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1) | |
| INSTRUCTION_MISSED=$(sed -n 's/.*type="INSTRUCTION".*missed="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1) | |
| BRANCH_COVERAGE=$(sed -n 's/.*type="BRANCH".*covered="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1) | |
| BRANCH_MISSED=$(sed -n 's/.*type="BRANCH".*missed="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1) | |
| # Calculate percentages | |
| INSTRUCTION_TOTAL=$((INSTRUCTION_COVERAGE + INSTRUCTION_MISSED)) | |
| BRANCH_TOTAL=$((BRANCH_COVERAGE + BRANCH_MISSED)) | |
| if [ $INSTRUCTION_TOTAL -gt 0 ]; then | |
| INSTRUCTION_PCT=$((INSTRUCTION_COVERAGE * 100 / INSTRUCTION_TOTAL)) | |
| else | |
| INSTRUCTION_PCT=0 | |
| fi | |
| if [ $BRANCH_TOTAL -gt 0 ]; then | |
| BRANCH_PCT=$((BRANCH_COVERAGE * 100 / BRANCH_TOTAL)) | |
| else | |
| BRANCH_PCT=0 | |
| fi | |
| echo "instruction_pct=$INSTRUCTION_PCT" >> $GITHUB_OUTPUT | |
| echo "branch_pct=$BRANCH_PCT" >> $GITHUB_OUTPUT | |
| # Check thresholds | |
| THRESHOLD_MET=true | |
| MESSAGES="" | |
| if [ $INSTRUCTION_PCT -lt 90 ]; then | |
| THRESHOLD_MET=false | |
| MESSAGES="${MESSAGES}❌ Overall instruction coverage is ${INSTRUCTION_PCT}% (threshold: 90%)\n" | |
| else | |
| MESSAGES="${MESSAGES}✅ Overall instruction coverage is ${INSTRUCTION_PCT}% (threshold: 90%)\n" | |
| fi | |
| if [ $BRANCH_PCT -lt 80 ]; then | |
| THRESHOLD_MET=false | |
| MESSAGES="${MESSAGES}❌ Branch coverage is ${BRANCH_PCT}% (threshold: 80%)\n" | |
| else | |
| MESSAGES="${MESSAGES}✅ Branch coverage is ${BRANCH_PCT}% (threshold: 80%)\n" | |
| fi | |
| echo "threshold_met=$THRESHOLD_MET" >> $GITHUB_OUTPUT | |
| echo -e "$MESSAGES" | |
| echo "messages<<EOF" >> $GITHUB_OUTPUT | |
| echo -e "$MESSAGES" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| if [ "$THRESHOLD_MET" = "false" ]; then | |
| exit 1 | |
| fi | |
| - name: Add coverage comment to PR | |
| uses: madrapps/[email protected] | |
| if: always() | |
| with: | |
| paths: | | |
| ${{ github.workspace }}/target/site/jacoco/jacoco.xml | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| min-coverage-overall: 90 | |
| min-coverage-changed-files: 80 | |
| title: '📊 Unit Test Coverage Report' | |
| update-comment: true | |
| - name: Upload JaCoCo coverage report | |
| uses: actions/upload-artifact@v4 | |
| if: always() | |
| with: | |
| name: jacoco-report | |
| path: target/site/jacoco/ | |
| - name: Fail if coverage thresholds not met | |
| if: steps.coverage-check.outputs.threshold_met == 'false' | |
| run: | | |
| echo "Coverage thresholds not met:" | |
| echo "${{ steps.coverage-check.outputs.messages }}" | |
| exit 1 | |