Disable transaction wrapping with @MicronautTest

Micronaut Test:

By default, when using @MicronautTest, each @Test method will be wrapped in a transaction that will be rolled back when the test finishes. This behaviour can be changed by using the transactional and rollback properties.

For example, if you create an application with data-jdbc, postgres and test-containers features with Micronaut Launch.

With an entity:

package com.example;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;

import javax.validation.constraints.NotBlank;

public class Book {
    private final String isbn;

    private final String name;

    public Book(@NonNull String isbn,
                @NonNull String name) {
        this.isbn = isbn;
        this.name = name;

    public String getIsbn() {
        return isbn;

    public String getName() {
        return name;

A JDBC repository:

package com.example;

import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface BookRepository extends CrudRepository<Book, String> {

And a Controller:

package com.example;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;

class BookController {

    private final BookRepository bookRepository;

    BookController(BookRepository bookRepository) {
        this.bookRepository = bookRepository;

    Number count() {
        return bookRepository.count();

The following test passes only if you set transactional=false with @MicronautTest.

package com.example;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@MicronautTest(transactional = false)
public class BookControllerTest {
    HttpClient httpClient;

    BookRepository bookRepository;

    void bookCount() {
        String title = "Building Microservices";
        String isbn = "1491950358";
        Book book = bookRepository.save(new Book(isbn, title));
        BlockingHttpClient client = httpClient.toBlocking();
        HttpRequest<?> request = HttpRequest.GET("/books/count")
        Integer count = client.retrieve(request, Integer.class);
        assertEquals(1, count);

Tags: #micronaut #micronaut-data #micronau-test #junit
Nov 2021, 22.


My next events:
🗓 Oct 05 17:00 Jumpstart your Java applications with AWS Lambda and Micronaut