Testing Architecture
Purpose: Comprehensive guide to testing strategy, approach, and execution in the StockEase project.
This document serves as the entry point for understanding how tests are organized, whatβs covered, and how to run them locally or in CI/CD.
Table of Contents
- Quick Start: Running Tests Locally
- Test Overview
- Test Structure
- Navigation by Role
- Test Artifact Locations
- Related Documentation
Quick Start: Running Tests Locally
Prerequisites
- JDK 17+
- Maven 3.8+
- Spring Boot 3.5.7
Run All Tests
cd backend
mvn clean testRun Specific Test Class
mvn test -Dtest=AuthControllerTestRun Tests with Coverage Report
mvn clean test jacoco:report
# Report generated at: backend/target/site/jacoco/index.htmlRun Integration Tests Only
mvn test -Dgroups=integrationView Coverage in Browser
# After running tests with coverage
open backend/target/site/jacoco/index.htmlTest Overview
Current State
- Total Test Classes: 9
- Total Test Methods: 65+ (including parameterized variants)
- Framework: JUnit 5 (Jupiter)
- Mocking: Mockito
- Test Profiles:
test(H2 in-memory database) - Coverage Target: 70%+ line coverage, 60%+ branch coverage
Test Categories
| Category | Count | Type | Framework |
|---|---|---|---|
| Unit Tests | 2 | Context bootstrap, core logic | Spring Boot Test |
| Controller Slice Tests | 7 | REST endpoint testing | @WebMvcTest |
| Total | 9 |
Technology Stack
JUnit 5 (Jupiter)
ββ Annotations: @Test, @BeforeEach, @ParameterizedTest
ββ Assertions: org.junit.jupiter.api
ββ Extensions: JUnit 5 extension model
Mockito 4.x
ββ @Mock: Field-level mocks
ββ @InjectMocks: Auto-wiring mocks
ββ ArgumentMatchers: any(), eq()
ββ Verification: when(), verify()
Spring Boot Test 3.5.7
ββ @SpringBootTest: Full context
ββ @WebMvcTest: Slice test for controllers
ββ @MockitoBean: Spring-aware bean mocking
ββ MockMvc: HTTP client simulation
Spring Security Test
ββ @WithMockUser: Mock authenticated users
ββ SecurityMockMvcRequestPostProcessors: CSRF tokens, roles
ββ JWT utilities: JwtUtil mock chains
Test Structure
Directory Layout
backend/src/test/
βββ java/com/stocks/stockease/
β βββ config/test/
β β βββ TestConfig.java # Shared test beans & security context
β βββ controller/
β β βββ AuthControllerTest.java # Auth endpoint tests (unit)
β β βββ ProductControllerTest.java # Product retrieval tests (@WebMvcTest)
β β βββ ProductCreateControllerTest.java # POST /api/products (@WebMvcTest)
β β βββ ProductDeleteControllerTest.java # DELETE tests (@WebMvcTest)
β β βββ ProductFetchControllerTest.java # GET tests (@WebMvcTest)
β β βββ ProductInvalidUpdateControllerTest.java # PUT validation (@WebMvcTest)
β β βββ ProductPaginationControllerTest.java # Pagination tests (@WebMvcTest)
β β βββ ProductUpdateControllerTest.java # PUT tests (@WebMvcTest)
β β βββ AuthControllerTest.java # Auth tests (unit)
β βββ StockEaseApplicationTests.java # Context bootstrap test
β
βββ resources/
βββ application-test.properties # H2 test database config
Test File Naming Convention
- Pattern:
{ClassName}Test.java(singular) - Examples:
AuthControllerTest,ProductControllerTest - Rationale: Clear ownership, easy discovery, follows Maven Surefire defaults
Navigation by Role
π¨βπΌ Product Manager / Business Analyst
- Start: Testing Strategy β understand goals and scope
- Then: Test Matrix β see whatβs covered by feature
- Finally: Check CI reports in
backend/target/surefire-reports/
π¨βπ» Backend Developer
- Start: Spring Slices β understand @WebMvcTest, mocking patterns
- Then: Security Tests β understand JWT/role testing
- Then: Test Data & Fixtures β mock setup
- Practice: Run
mvn clean testlocally and explorebackend/src/test/java/
π¨βπ» Frontend Developer
- Start: Coverage & Quality β understand backend test status
- Then: Security Tests β understand auth token validation
- Reference: Links to API contract tests (when available)
π§ DevOps / CI-CD Engineer
- Start: CI Pipeline Tests β what runs in GitHub Actions
- Then: Coverage & Quality β threshold gates
- Finally: Check
.github/workflows/ci-build.ymlfor test stage execution
π§ͺ QA / Test Engineer
- Start: Test Pyramid β understand test breakdown
- Then: Coverage Matrix β see coverage by layer/feature
- Then: Naming Conventions β understand test structure
- Practice: Review test classes in
backend/src/test/java/com/stocks/stockease/
Test Artifact Locations
Source Code
| Path | Purpose |
|---|---|
backend/src/test/java/com/stocks/stockease/ |
All test classes |
backend/src/test/java/com/stocks/stockease/controller/ |
REST endpoint tests |
backend/src/test/java/com/stocks/stockease/config/test/ |
Shared test configuration |
backend/src/test/resources/ |
Test data, properties files |
Generated Reports (Post-Build)
| Report | Location | Command |
|---|---|---|
| JUnit XML | backend/target/surefire-reports/TEST-*.xml |
mvn test |
| JUnit Text | backend/target/surefire-reports/*.txt |
mvn test |
| JaCoCo HTML | backend/target/site/jacoco/index.html |
mvn test jacoco:report |
| Compiled Tests | backend/target/test-classes/com/stocks/stockease/ |
mvn test-compile |
CI/CD Pipeline
| File | Purpose | Trigger |
|---|---|---|
.github/workflows/ci-build.yml |
Full test + build pipeline | Push to main / PR |
| Surefire Reports Archive | Test results artifact | After mvn test |
| JaCoCo Report Archive | Coverage HTML artifact | After mvn jacoco:report |
Test Pyramid
β²
β±ββ²
β± β β² 5% System/E2E
β± β β² (Future: full-stack UI tests)
β±ββββββΌββββββ²
β± β β²
β± β β² 25% Integration/Slice
β± β β² (7 controller tests w/ @WebMvcTest)
β±ββββββββββΌββββββββββ²
β± β β²
β± β β² 70% Unit
β± β β² (1 app context test, isolated mocks)
β±ββββββββββββββ΄ββββββββββββββ²
β± β²
β±___________Unit Tests___________β²
Current Ratio: ~70% unit, ~25%
slice/integration, ~5% system
Target Ratio: Same (balanced across
layers)
Key Testing Patterns
Pattern 1: @WebMvcTest (Controller Slicing)
- Used in: 7 controller test classes
- What it does: Loads only the web layer + security config
- Benefits: Fast execution, no database or service layer
- Example:
ProductControllerTest.java
@WebMvcTest(ProductController.class)
@ExtendWith(MockitoExtension.class)
public class ProductControllerTest {
@Autowired private MockMvc mockMvc;
@MockitoBean private ProductRepository productRepository;
// Tests use: mockMvc.perform(get(...))
}Pattern 2: @SpringBootTest (Full Context)
- Used in:
StockEaseApplicationTests.java - What it does: Loads full application context
- Benefits: Tests real bean wiring, integration scenarios
- Caution: Slower execution
@SpringBootTest
@ActiveProfiles("test")
class StockEaseApplicationTests {
@Test
void contextLoads() { /* Spring boots successfully */ }
}Pattern 3: MockitoBean Auto-Wiring
- Used in: All controller tests
- What it does: Injects mocked beans into Spring context
- Pattern:
@MockitoBean private Repository repository;
Pattern 4: Parameterized Tests
- Used in:
ProductControllerTest,ProductCreateControllerTest - Framework:
@ParameterizedTestwith@CsvSource - Benefit: Test multiple scenarios with one method
@ParameterizedTest
@CsvSource({
"adminUser, ADMIN",
"regularUser, USER"
})
void testWithRoles(String username, String role) { }Pattern 5: Test Configuration Inheritance
- Class:
TestConfig.java - Provides: Mock
JwtUtil, mockUserDetailsService, pre-configuredSecurityContext - Usage:
@Import(TestConfig.class)in test classes
Quick Reference: Test Execution Flow
1. Local Development
ββ mvn clean test
ββ Compiles tests
ββ Loads application-test.properties (H2 database)
ββ Runs 9 test classes
ββ Generates JUnit reports
ββ Success β
2. With Coverage
ββ mvn clean test jacoco:report
ββ Runs all tests (as above)
ββ Instruments bytecode for coverage
ββ Generates HTML report
ββ Open: backend/target/site/jacoco/index.html
3. CI/CD Pipeline (GitHub Actions)
ββ git push main
ββ Triggers .github/workflows/ci-build.yml
ββ Runs mvn clean verify
ββ Publishes results to GitHub
ββ Blocks merge if tests fail
Related Documentation
Testing Topics
- Testing Strategy β Goals, scope, and test philosophy
- Test Pyramid β Unit/slice/integration breakdown
- Coverage Matrix β Whatβs tested by layer and feature
- Naming Conventions β Test method naming + GWT patterns
Testing Techniques
- Spring Slices β @WebMvcTest, @DataJpaTest patterns
- Security Tests β JWT, role validation, auth flows
- Test Data & Fixtures β Mock setup, TestConfig, builders
- Controller Integration Tests β MockMvc patterns
Quality & Metrics
- Coverage & Quality β JaCoCo thresholds, must-cover classes
- Coverage Matrix β Whatβs tested by layer and feature
- Test Pyramid β Unit/slice/integration breakdown
- CI Pipeline Tests β What runs in GitHub Actions
Architecture Overview
- Architecture Overview β Main system architecture
- Backend Architecture β Spring Boot layers and components
- Security Architecture β JWT, authentication, authorization
- Deployment β CI/CD and production setup
Last Updated: October 31, 2025
Version: 1.0
Status: β
Ready for review