Skip to content

Test: Added Unit and Integration Tests for note-related features#13

Merged
TUT888 merged 15 commits intodevelopfrom
feature/notes-basic-crud
Dec 30, 2025
Merged

Test: Added Unit and Integration Tests for note-related features#13
TUT888 merged 15 commits intodevelopfrom
feature/notes-basic-crud

Conversation

@TUT888
Copy link
Owner

@TUT888 TUT888 commented Dec 10, 2025

Summary

This PR establishes a testing infrastructure for the SmartNotes backend, focusing on Note-related features.

It introduces a clear separation between unit and integration tests, leverages Testcontainers for realistic integration testing, and improves test readability with tree-structured reporting.

Updates on 17 Dec 2025:

  • Set up GitHub Actions workflows and the initial workflow strategy
  • Added automatic tests triggered when commits are pushed to specific feature branches and when PRs target the develop branch
  • Added feature tagging for test files, allowing selective testing based on tags

Overview

Test Coverage

  • Unit tests for NoteService (business logic with mocked dependencies)
  • Integration tests for DocumentRepository (using in‑memory H2 database by default)
  • Integration tests for NoteController (using MySQL and Redis containers via Testcontainers)

Configuration

  • Testcontainers configured with reuse enabled for efficient integration testing
  • Tree‑structured reporting for clearer, hierarchical test output
  • Separate Maven profiles for unit and integration tests

CI/CD Workflows

  • Unit tests for note‑related features are triggered whenever changes are made to feature branches that include the note keyword
  • All unit and integration tests are triggered whenever pull requests are made to the develop branch

Detailed Changes

Document Repository Changes

Replaced the existing findByTitleAndUserId method with findFirstByTitleAndUserId to fix an error caused when multiple documents with the same title were returned. This ensures that only the first matching document is mapped to a single result.

Note-related Features Testing

Test Organization

  • Created unit/ and integration/ directories for maintainability:
    • Unit tests: Fast tests with mocked dependencies, focusing on business logic (Surefire).
    • Integration tests: Full-stack tests with real database and containers (Failsafe).
  • Added support classes in helper/:
    • DocumentDataBuilder and UserDataBuilder: generate customizable sample objects for repositories.
    • JwtBuilder: builds JWT tokens for controller tests requiring authentication (full login flow will be tested separately).
  • Structured test classes with nested classes to group cases by method/functionality.
    Example: NoteServiceTest.GetNoteTest covers scenarios such as:
    • Throw exception if note not found
    • Return note response if note exists

Test Options

  • Test Profiles: Added 2 new profiles to run specific test type
    • unit-test: runs all tests in unit/ directory (*Test.java)
    • integration-test: runs all tests in integration/ directory (*Test.java)
    • test: runs all tests (unit + integration)
  • Feature‑based Testing: Added tags to selectively run groups of tests based on specific tags:
    • Note‑related test files are marked with the class‑level @Tag("note") annotation
    • Document‑related test files are marked with the class‑level @Tag("document") annotation

CI/CD with GitHub Actions

All files are placed in the .github directory. For more details, refer to .github/workflows/README.md.

  • Added workflows/_reusable-test.yml, which can be called in other workflows to run tests based on given input
  • Added workflows/feature-test.yml, which is triggered by changes to source code (src/) on feature branches:
    • Runs scripts/detect-branch-name.sh to detect the branch name (including the note keyword) and specify the test tag to be triggered
    • Runs workflows/_reusable-test.yml to execute all unit tests
  • Added workflows/ci-cd-develop.yml, which is triggered when a PR is created for the develop branch. It runs all unit and integration tests, and also builds and pushes images to Docker Hub (placeholder only, to be implemented later).

Documentation

  • Updated README with improved formatting, contributor section, and link to frontend repository.
  • Added README for GitHub Actions Workflow

Instructions

Running Test Locally

Before running tests:

  • Ensure Docker is running for integration tests.
  • If test code has changed, add clean before test to rebuild.

Run with IntelliJ IDEA

Use the run button on individual test methods or classes.

Run with commands (Windows)

# Unit tests (fast)
./mvnw.cmd test -Punit-test

# Integration tests (comprehensive)
./mvnw.cmd verify -Pintegration-test

# All tests
./mvnw.cmd verify

Optionally run a specific test group with tags. Use boolean expressions for tag combinations:

# Run test with "note" tag
./mvnw.cmd test -Dgroups="note"

# Run tests with "note" or "document" tags
./mvnw.cmd test -Dgroups="note | document"

# Combine with test type: run all unit tests with "note" tag
./mvnw.cmd test -Punit-test -Dgroups="note | document"

Upcomming Works

Test other features

All tests should be labelled with JUnit Tag using @Tag() annotation

  • Setup unit test and/or integration tests for other features
  • Add other higher-level testing (e.g. end-to-end)

Continue CI/CD Setup

Extra check for CI (linting and security checks, etc):

  • Whether add new jobs in workflows/_reusable-test.yml, or create new reusable workflow
  • Call this workflow in other main workflow to run the check

On changes to feature branch (feature-test.yml)

  • Update the scripts/detect-branch-name.sh, mapping the branch name to defined JUnit Tag.

On PR to develop branch (ci-cd-develop.yml)

  • Build Docker Images
  • Deployment to Docker Hub and AWS

@TUT888 TUT888 requested a review from pvdev1805 December 10, 2025 04:02
@TUT888 TUT888 removed the request for review from pvdev1805 December 30, 2025 11:00
@TUT888 TUT888 merged commit d15340f into develop Dec 30, 2025
2 checks passed
@TUT888
Copy link
Owner Author

TUT888 commented Dec 30, 2025

Proceeding with merge as the collaborator is temporarily unavailable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant