0
0
JUnittesting~8 mins

Spring Boot @SpringBootTest in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Spring Boot @SpringBootTest
Folder Structure
spring-boot-app/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/app/
│   │   │       ├── Application.java
│   │   │       ├── controller/
│   │   │       ├── service/
│   │   │       └── repository/
│   │   └── resources/
│   │       └── application.properties
│   └── test/
│       ├── java/
│       │   └── com/example/app/
│       │       ├── controller/
│       │       │   └── ControllerIntegrationTest.java
│       │       ├── service/
│       │       │   └── ServiceUnitTest.java
│       │       └── repository/
│       │           └── RepositoryTest.java
│       └── resources/
│           └── application-test.properties
├── pom.xml
└── README.md
Test Framework Layers
  • Test Runner Layer: Uses JUnit 5 to run tests annotated with @SpringBootTest.
  • Application Context Layer: Loads Spring Boot application context for integration testing.
  • Test Classes: Located under src/test/java, organized by package matching main code.
  • Page Objects / Test Utilities: Helper classes for reusable test logic, mocks, or test data builders.
  • Configuration Layer: Uses application-test.properties for test-specific settings.
Configuration Patterns
  • Profiles: Use Spring profiles like test activated via @ActiveProfiles("test") to load test-specific configs.
  • Properties: Separate application-test.properties for database URLs, ports, or mock endpoints.
  • Embedded Databases: Use H2 or similar in-memory DB for fast, isolated tests.
  • Mock Beans: Use @MockBean to replace real beans with mocks during tests.
  • Environment Variables: Override sensitive data like credentials via environment or CI secrets.
Test Reporting and CI/CD Integration
  • JUnit Reports: Generate XML reports automatically via Maven Surefire or Gradle test tasks.
  • CI/CD Pipelines: Integrate tests in Jenkins, GitHub Actions, or GitLab CI to run on every commit.
  • Code Coverage: Use JaCoCo plugin to measure test coverage and fail builds if coverage drops.
  • Test Logs: Configure logging to capture detailed test execution info for debugging failures.
  • Test Tags: Use JUnit tags to group and selectively run integration tests.
Best Practices
  1. Use @SpringBootTest for Integration Tests: It loads full context, so use it only when needed to avoid slow tests.
  2. Isolate Tests: Use embedded databases and mock external services to keep tests reliable and repeatable.
  3. Organize Tests by Layer: Separate unit tests from integration tests in folder structure and naming.
  4. Use Profiles and Properties: Keep test configuration separate from production to avoid side effects.
  5. Clean Context Between Tests: Use @DirtiesContext sparingly to reset context if tests modify shared state.
Self Check

Where would you add a new integration test class for a REST controller in this framework structure?

Key Result
Use @SpringBootTest in src/test/java with test profiles and embedded DB for reliable integration tests.