Write Tests
Write unit and integration tests: $ARGUMENTS
Instructions
Follow this systematic approach to write effective tests:
- •
Test Framework Detection
- •Identify the testing framework in use (JUnit, Jest, Mocha, PyTest, RSpec, etc.)
- •Review existing test structure and conventions
- •Check test configuration files and setup
- •Understand project-specific testing patterns
- •
Code Analysis for Testing
- •Analyze the code that needs testing
- •Identify public interfaces and critical business logic
- •Map out dependencies and external interactions
- •Understand error conditions and edge cases
- •Ensure that line and branch coverage is more than 80%, and add test cases to cover uncovered lines and branches
- •
Test Strategy Planning
- •Determine test levels needed:
- •Unit tests for individual functions/methods
- •Integration tests for component interactions
- •End-to-end tests for user workflows
- •Plan test coverage goals and priorities
- •Identify mock and stub requirements
- •Determine test levels needed:
- •
Unit Test Implementation
- •Test individual functions and methods in isolation
- •Cover happy path scenarios first
- •Test edge cases and boundary conditions
- •Test error conditions and exception handling
- •Always use the AssertJ assertions and matchers for Spring Boot codebase
- •Always add
@DisplayNamewith human readable description to test methods in Java codebase - •Do not include Java test case method name in the
@DisplayName - •Follow the convention
{methodName}_{precondition}_{expectedOutcome}for the Java test case method names - •Prefer using
@InjectMocks(if possible) over instantiating the tested class in the setup method@BeforeEach - •Follow the naming convention of naming instances of the tested class as
subject- subject under test - •Never create tests for the JPA/Hibernate entities, only for the business logic and services
- •Never create tests for the DTOs and Spring Boot
@Configurationbeans - •Do not create tests for the Spring Data repositories, only for the business logic and services
- •Prefer using Java’s
varfor local variables that are initialized by a constructor or builder call - •Always follow the best practices for using constants in test matchers and assertions:
- •Use constants for expected values that are used in multiple test cases
- •Avoid using constants for values that are only used in a single test case
- •Use descriptive names for constants to improve readability
- •
Test Structure and Organization
- •Follow the AAA pattern (Arrange, Act, Assert)
- •DO NOT WRITE obvious comments like "Arrange, Act, Assert" or "Given, When, Then" - use empty lines instead
- •Use descriptive test names that explain the scenario
- •Group related tests using test suites/describe blocks
- •Keep tests focused and atomic
- •
Mocking and Stubbing
- •Mock external dependencies and services
- •Stub complex operations for unit tests
- •Use proper isolation for reliable tests
- •Avoid over-mocking that makes tests brittle
- •
Data Setup and Teardown
- •Create test fixtures and sample data
- •Set up and tear down test environments cleanly
- •Use factories or builders for complex test data
- •Ensure tests don't interfere with each other
- •
Integration Test Writing
- •Test component interactions and data flow
- •Test API endpoints with various scenarios
- •Test database operations and transactions
- •Test external service integrations
- •
Error and Exception Testing
- •Test all error conditions and exception paths
- •Verify proper error messages and codes
- •Test error recovery and fallback mechanisms
- •Test validation and security scenarios
- •
Performance and Load Testing
- •Add performance tests for critical operations
- •Test under different load conditions
- •Verify memory usage and resource cleanup
- •Test timeout and rate limiting scenarios
- •
Security Testing
- •Test authentication and authorization
- •Test input validation and sanitization
- •Test for common security vulnerabilities
- •Test access control and permissions
- •
Accessibility Testing (for UI)
- •Test keyboard navigation and screen readers
- •Test color contrast and visual accessibility
- •Test ARIA attributes and semantic markup
- •Test with assistive technology simulations
- •
Cross-Platform Testing
- •Test on different operating systems
- •Test on different browsers (for web apps)
- •Test on different device sizes and resolutions
- •Test with different versions of dependencies
- •
Test Utilities and Helpers
- •Create reusable test utilities and helpers
- •Build test data factories and builders
- •Create custom matchers and assertions
- •Set up common test setup and teardown functions
- •
Snapshot and Visual Testing
- •Use snapshot testing for UI components
- •Implement visual regression testing
- •Test rendered output and markup
- •Version control snapshots properly
- •
Async Testing
- •Test asynchronous operations properly
- •Use appropriate async testing patterns
- •Test promise resolution and rejection
- •Test callback and event-driven code
- •
Test Documentation
- •Document complex test scenarios and reasoning
- •Add comments for non-obvious test logic
- •Create test documentation for team reference
- •Document test data requirements and setup
- •
Test Maintenance
- •Keep tests up to date with code changes
- •Refactor tests when code is refactored
- •Remove obsolete tests and update assertions
- •Monitor and fix flaky tests
Remember to prioritize testing critical business logic and user-facing functionality first, then expand coverage to supporting code.