How to start Spring Boot app in Spock Integration Test
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you want a Spock test to exercise the real Spring Boot application, you should start the full application context instead of mocking the web layer by hand. In practice that means using @SpringBootTest, choosing an appropriate web environment, and writing the specification so Spring can inject the beans or HTTP client you need.
Basic Spock Integration Test Setup
A full integration test with an embedded server usually looks like this:
@SpringBootTest tells Spring Boot to start the application the same way it would for normal runtime. RANDOM_PORT is a safe default because it avoids collisions with other processes during test runs.
Dependencies You Usually Need
For Gradle, the essential testing dependencies are Spring Boot test support and Spock.
The exact Spock version should match your Groovy version. That version alignment matters more than most test-setup guides admit.
Choosing the Right Test Style
Not every integration test needs a running HTTP server.
Use webEnvironment = NONE when you only want the application context and beans:
Use RANDOM_PORT when you want to test HTTP behavior end to end. Use MOCK when you want Spring MVC infrastructure without a real embedded container.
Isolating Test Configuration
Integration tests should usually run with a dedicated profile.
Then create application-test.yml with a test database or fake external endpoints. That keeps the test bootstrapping realistic without pointing at production services.
Testing Database-Backed Behavior
If the application touches a database, make the test environment explicit. For small projects, an in-memory database is often enough. For production-like behavior, a containerized database is usually more honest.
A service-level integration spec can still use the same bootstrapping approach:
The important part is not the assertion. It is that the full application wiring, transactions, and configuration are exercised together.
Keeping Startup Predictable
When integration tests boot the application, remove anything non-essential from the startup path. Scheduled jobs, message consumers, and calls to real third-party services make tests slower and less deterministic. A test profile can disable those pieces while still starting the same Spring Boot application shape that production uses.
That balance is the point of the test: real bootstrapping, controlled dependencies.
Common Pitfalls
The most common failure is missing spock-spring, which prevents Spring-specific annotations from behaving correctly.
Another common issue is using an incompatible Spock and Groovy combination. If the versions do not line up, the application may fail before any test runs.
A third pitfall is using a fixed port instead of RANDOM_PORT, which makes tests flaky on CI when the port is already in use.
Summary
- Use
@SpringBootTestto start the real Spring Boot application in a Spock integration test. - '
RANDOM_PORTis the safest option for full HTTP integration tests.' - Add both
spock-coreandspock-spring, and keep them aligned with Groovy. - Use
webEnvironment = NONEwhen you only need the Spring context, not a server. - Isolate integration test settings with a dedicated Spring profile.
Related reading
- How to start up spring-boot application via command line?
- How to start up spring-boot application via command line?
- How to stop a thread created by implementing runnable interface?
- How to store printStackTrace into a string
- How to stratify the training and testing data in Scikit-Learn?
- How to suppress py.test internal deprecation warnings
- How To Stream Chunked Response With Spring Boot RestController
- How to subtract X day from a Date object in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.