Micronaut integration testing with embedded server

When building REST APIs or microservices in Micronaut, unit tests validate individual components.
But real-world applications need integration tests to ensure everything works together — routes, controllers, services, repositories, security, and more.

✅ Micronaut provides excellent built-in support for integration testing using its Embedded Server.

  • No external deployment required.
  • Fast startup.
  • Lightweight but production-like environment.

In this post, you’ll learn step-by-step how to write integration tests using Embedded Server.

Micronaut integration testing with embedded server

Why Use Embedded Server for Integration Tests?

ProblemSolution
Hard to test full stackEmbedded server simulates full app
Need HTTP-level testingEmbedded server exposes real HTTP endpoints
Test routes/controllers/servicesAll are active and wired

With Micronaut’s Embedded Server:

  • Tests run inside the Micronaut runtime.
  • Full dependency injection works.
  • Real HTTP clients can call actual routes.

🚀 Project Setup

Let’s assume you already have a basic Micronaut project.

If not, you can generate one via Micronaut CLI:

🛠 Maven Dependencies

Micronaut testing support is included by default.
Verify these dependencies exist:

If you’re using Spock, replace with micronaut-test-spock.

🏫 Example Application

We’ll test this simple controller:

🧪 Writing Integration Tests

Let’s create a test class that:

  • Boots Micronaut using Embedded Server.
  • Calls the REST endpoint.
  • Asserts the response.

✅ Example Integration Test

✅ What’s happening here:

  • @MicronautTest — boots Embedded Server automatically.
  • @Client("/") — injects an HTTP client targeting the running server.
  • Actual HTTP request made via retrieve().
  • Response asserted with JUnit.

The server runs in-memory, so you don’t need external deployment.

⚙ Configuration for Embedded Server

Micronaut automatically uses application-test.yml profile when running tests.

You can override config for integration tests like:

✅ This isolates your tests from production configuration.

🧪 Testing Services and Repositories

Micronaut fully wires all beans during integration tests.

Example test with service:

✅ You can inject any bean like controllers, services, repositories, even external clients.

🔬 Testing Full CRUD Integration

If you have database access via repositories, you can write full integration tests:

✅ Micronaut supports in-memory H2 by default, making DB integration tests fast.

⚠ Common Mistakes to Avoid

IssueSolution
@MicronautTest not workingEnsure micronaut-test-junit5 is in dependencies
HTTP client failsUse @Client("/") to target embedded server
Database not availableUse in-memory H2 or test containers

🧰 Best Practices

✅ Use Embedded Server for real HTTP-level tests
✅ Use application-test.yml for isolation
✅ Use transactional tests unless testing async behavior
✅ Avoid hitting external services (mock them if needed)
✅ Use lightweight test data

🌐 External References