From 272c3e303f0acbf4f559a7f67544593c9b80abff Mon Sep 17 00:00:00 2001 From: Hendrik Ebbers Date: Wed, 15 Jul 2026 15:24:02 +0200 Subject: [PATCH] fix(tests): update mock annotations and pre-create schema for PostgreSQL container --- .../java/com/example/aggregate/AggregateTestConfig.java | 5 ++++- .../src/test/resources/db/create-schema.sql | 5 +++++ .../user/UserServiceActiveGateIntegrationTest.java | 6 +++--- .../base/security/user/UserServiceConcurrencyTest.java | 6 +++--- .../services/apikey/ApiKeyDataServiceIntegrationTest.java | 8 ++++---- .../base/services/audit/AuditLogIntegrationTest.java | 6 +++--- .../base/services/tag/TagDataServiceIntegrationTest.java | 6 +++--- .../webhook/data/WebhookDataServiceIntegrationTest.java | 6 +++--- .../base/testcontainers/PostgresTestConfiguration.java | 5 ++++- .../test/resources/application-testcontainers.properties | 7 +++++-- .../src/test/resources/db/create-schema.sql | 5 +++++ 11 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 spring-services-all/src/test/resources/db/create-schema.sql create mode 100644 spring-services-core/src/test/resources/db/create-schema.sql diff --git a/spring-services-all/src/test/java/com/example/aggregate/AggregateTestConfig.java b/spring-services-all/src/test/java/com/example/aggregate/AggregateTestConfig.java index a5c8358..9b2c070 100644 --- a/spring-services-all/src/test/java/com/example/aggregate/AggregateTestConfig.java +++ b/spring-services-all/src/test/java/com/example/aggregate/AggregateTestConfig.java @@ -17,7 +17,10 @@ public class AggregateTestConfig { @Bean @ServiceConnection public PostgreSQLContainer postgresContainer() { - return new PostgreSQLContainer<>("postgres:16-alpine"); + return new PostgreSQLContainer<>("postgres:16-alpine") + // Pre-create the dedicated schema so Hibernate's create-drop startup DROP phase does not + // log a CommandAcceptanceException for the not-yet-existing oe_spring_services schema. + .withInitScript("db/create-schema.sql"); } @Bean diff --git a/spring-services-all/src/test/resources/db/create-schema.sql b/spring-services-all/src/test/resources/db/create-schema.sql new file mode 100644 index 0000000..df56459 --- /dev/null +++ b/spring-services-all/src/test/resources/db/create-schema.sql @@ -0,0 +1,5 @@ +-- Pre-create the library's dedicated schema so Hibernate's create-drop startup DROP phase +-- (which emits an unguarded "drop schema oe_spring_services") has an existing schema to drop +-- on the first run against a fresh Testcontainer. Without this, Hibernate logs a harmless but +-- noisy CommandAcceptanceException before recreating the schema. +CREATE SCHEMA IF NOT EXISTS oe_spring_services; diff --git a/spring-services-core/src/test/java/com/openelements/spring/base/security/user/UserServiceActiveGateIntegrationTest.java b/spring-services-core/src/test/java/com/openelements/spring/base/security/user/UserServiceActiveGateIntegrationTest.java index 8612764..bf0f32a 100644 --- a/spring-services-core/src/test/java/com/openelements/spring/base/security/user/UserServiceActiveGateIntegrationTest.java +++ b/spring-services-core/src/test/java/com/openelements/spring/base/security/user/UserServiceActiveGateIntegrationTest.java @@ -19,7 +19,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.context.annotation.Import; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.security.access.AccessDeniedException; @@ -49,7 +49,7 @@ * transactional configuration — entities are flushed and reloaded across transactions so the * assertions reflect what is durably in the database, not just the session cache. * - *

Mock-Audit. A single {@code @MockBean AuthService} is the only mock. The real + *

Mock-Audit. A single {@code @MockitoBean AuthService} is the only mock. The real * {@code AuthService} would require a Spring Security {@code SecurityContextHolder} populated by * the filter chain, which these tests deliberately bypass to drive {@code getCurrentUserEntity()} * directly. The {@code UserService}, {@link UserRepository}, and {@code UserProvisioner} under @@ -68,7 +68,7 @@ class UserServiceActiveGateIntegrationTest { @Autowired private JdbcTemplate jdbcTemplate; - @MockBean private AuthService authService; + @MockitoBean private AuthService authService; /** * Deletes dependent {@code audit_log} rows before the {@code users} rows they reference, then diff --git a/spring-services-core/src/test/java/com/openelements/spring/base/security/user/UserServiceConcurrencyTest.java b/spring-services-core/src/test/java/com/openelements/spring/base/security/user/UserServiceConcurrencyTest.java index 3d35b76..0a97cd2 100644 --- a/spring-services-core/src/test/java/com/openelements/spring/base/security/user/UserServiceConcurrencyTest.java +++ b/spring-services-core/src/test/java/com/openelements/spring/base/security/user/UserServiceConcurrencyTest.java @@ -31,7 +31,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.context.annotation.Import; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ActiveProfiles; @@ -69,7 +69,7 @@ * in-memory database faithfully reproduces. * *

Mock surface and justification. {@link - * com.openelements.spring.base.security.AuthService} is the single {@code @MockBean} — it would + * com.openelements.spring.base.security.AuthService} is the single {@code @MockitoBean} — it would * otherwise require a Spring Security {@code SecurityContextHolder} populated by the filter * chain, which the tests deliberately bypass to drive {@code getCurrentUserEntity()} directly * on background threads. Per-thread {@code UserInformation} dispatch is implemented with a @@ -100,7 +100,7 @@ class UserServiceConcurrencyTest { @Autowired private JdbcTemplate jdbcTemplate; - @MockBean private AuthService authService; + @MockitoBean private AuthService authService; /** * Deletes dependent {@code audit_log} rows before the {@code users} rows they reference, then diff --git a/spring-services-core/src/test/java/com/openelements/spring/base/services/apikey/ApiKeyDataServiceIntegrationTest.java b/spring-services-core/src/test/java/com/openelements/spring/base/services/apikey/ApiKeyDataServiceIntegrationTest.java index 5519066..f8bd122 100644 --- a/spring-services-core/src/test/java/com/openelements/spring/base/services/apikey/ApiKeyDataServiceIntegrationTest.java +++ b/spring-services-core/src/test/java/com/openelements/spring/base/services/apikey/ApiKeyDataServiceIntegrationTest.java @@ -18,7 +18,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.context.annotation.Import; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -46,7 +46,7 @@ * service, and the {@link ApiKeyRepository} is the real Spring Data repository writing to the * containerised database. * - *

Mock-Audit. Two {@code @MockBean}s: + *

Mock-Audit. Two {@code @MockitoBean}s: * *