wasAsked.

Describe a practical unit, integration, contract, and end-to-end test strategy for a Java full-stack product.

BrowserStack · Full Stack Developer · 3-4-years · technical

Published

Year not specified · Java, React, Testing

Community answers

Posted by wasAsked editorial collection

Use a risk-based test pyramid: many fast tests at the bottom, fewer slow tests at the top. Unit tests - Backend: JUnit 5 with Mockito (and AssertJ) for services, domain logic, validators and mappers. No Spring context — plain object construction keeps tests fast. - Frontend: Vitest or Jest with React Testing Library for components, hooks and utilities, testing behaviour visible to the user. Integration tests (backend) - Spring Boot test slices: @WebMvcTest for controllers (validation, error handling, security rules with spring-security-test), @DataJpaTest for repositories and queries. - Full @SpringBootTest with Testcontainers for PostgreSQL or MySQL, Kafka or RabbitMQ and Redis, so tests run against real infrastructure versions instead of H2, which behaves differently. Spring Boot's @ServiceConnection simplifies wiring containers. - Verify migrations (Flyway or Liquibase) run correctly. - Stub external HTTP services with WireMock, including timeouts and error responses. Contract tests - Consumer-driven contracts with Pact or Spring Cloud Contract between the React app and the API, or between microservices, so breaking API changes are caught before deployment. - Alternatively, validate responses against the OpenAPI specification (springdoc-openapi) and diff specs in pull requests. End-to-end tests - Playwright or Cypress against a deployed test environment, covering a small set of critical journeys: login, search, checkout, payment and permissions. - Use stable selectors (data-testid or accessible roles), seeded test data, and waits based on application state rather than fixed sleeps. Pipeline integration - Unit and integration tests on every pull request; contract verification in both consumer and provider pipelines; E2E tests after deploying to the test or staging environment. - Coverage reports (JaCoCo, Vitest coverage) as guidance, with quality gates on new code. - Track flaky tests and fix them quickly. Additional checks: accessibility (axe), performance smoke tests (k6 or Gatling), security scanning (OWASP dependency check, ZAP), and a regression test for every production bug.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics