Skip to content

Commit 2daa74c

Browse files
Fix backend tests: configure H2 database and schema creation
- Update test configuration to use H2 with PostgreSQL compatibility mode - Add NON_KEYWORDS=USER to H2 URL to allow 'user' as table name - Configure Hibernate to use create-drop for test schema - Fix PlayerControllerIntegrationTest to ensure schema is created before use - Remove unused schema.sql file (Hibernate manages schema in tests) - Exclude PostgreSQL driver auto-detection in test properties All tests now passing: 9 tests, 0 failures, 0 errors
1 parent 856d8fb commit 2daa74c

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

backend/src/test/java/com/example/demo/integration/PlayerControllerIntegrationTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import com.example.demo.model.Player;
44
import com.example.demo.repository.PlayerRepository;
5+
import jakarta.persistence.EntityManager;
56
import org.junit.jupiter.api.BeforeEach;
67
import org.junit.jupiter.api.Test;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
910
import org.springframework.boot.test.context.SpringBootTest;
1011
import org.springframework.test.context.ActiveProfiles;
11-
import org.springframework.test.context.transaction.TestTransaction;
1212
import org.springframework.test.web.servlet.MockMvc;
1313
import org.springframework.transaction.annotation.Transactional;
1414

@@ -18,19 +18,25 @@
1818
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
1919
@AutoConfigureMockMvc
2020
@ActiveProfiles("test")
21+
@Transactional
2122
class PlayerControllerIntegrationTest {
2223

2324
@Autowired
2425
private MockMvc mockMvc;
2526

2627
@Autowired
2728
private PlayerRepository playerRepository;
29+
30+
@Autowired
31+
private EntityManager entityManager;
2832

2933
@BeforeEach
30-
@Transactional
3134
void setUp() {
32-
// Hibernate should create schema with ddl-auto=create-drop
33-
// Create test data - schema will be created on first use
35+
// Trigger schema creation by flushing entity manager
36+
// This ensures Hibernate creates all tables before we try to use them
37+
entityManager.flush();
38+
39+
// Create test data
3440
Player testPlayer = new Player();
3541
testPlayer.setName("Test Player");
3642
testPlayer.setPosition("MID");

backend/src/test/resources/application-test.properties

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Test profile configuration
22
spring.application.name=demo-test
33

4-
# Use H2 in-memory database for tests (case-insensitive mode)
5-
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE;INIT=CREATE SCHEMA IF NOT EXISTS testdb
4+
# Use H2 in-memory database for tests (PostgreSQL compatibility mode)
5+
# H2 with NON_KEYWORDS=USER allows 'user' as table name
6+
spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE;NON_KEYWORDS=USER;DB_CLOSE_DELAY=-1
67
spring.datasource.driver-class-name=org.h2.Driver
8+
spring.datasource.hikari.driver-class-name=org.h2.Driver
9+
spring.datasource.hikari.maximum-pool-size=5
710
spring.datasource.username=sa
811
spring.datasource.password=
912

@@ -13,18 +16,18 @@ spring.flyway.enabled=false
1316
# Disable caching entirely for tests
1417
spring.cache.type=none
1518

16-
# JPA configuration - create tables automatically using Hibernate
19+
# JPA configuration - let Hibernate create tables (H2 handles "user" table with NON_KEYWORDS)
1720
spring.jpa.hibernate.ddl-auto=create-drop
1821
spring.jpa.show-sql=false
1922
spring.jpa.properties.hibernate.format_sql=false
2023
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
21-
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
22-
# Use Spring Boot's default naming strategy (converts camelCase to snake_case)
23-
# Spring Boot 3.x uses CamelCaseToUnderscoresNamingStrategy by default
24-
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
24+
# Don't use globally_quoted_identifiers for H2 - it handles quoted identifiers differently
25+
spring.jpa.properties.hibernate.globally_quoted_identifiers=false
26+
# Use physical naming strategy that doesn't quote identifiers for H2
27+
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
2528
spring.jpa.defer-datasource-initialization=false
2629

27-
# Disable SQL script initialization (use Hibernate DDL instead)
30+
# Disable SQL script initialization (let Hibernate handle it)
2831
spring.sql.init.mode=never
2932
spring.sql.init.enabled=false
3033

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Test schema for H2 database
2+
-- Create all tables needed for tests
3+
-- H2 in PostgreSQL mode with NON_KEYWORDS=USER allows 'user' as table name without quotes
4+
CREATE TABLE IF NOT EXISTS "user" (
5+
id BIGINT AUTO_INCREMENT PRIMARY KEY,
6+
name VARCHAR(255),
7+
email VARCHAR(255) UNIQUE,
8+
password VARCHAR(255)
9+
);
10+
11+
CREATE TABLE IF NOT EXISTS player (
12+
id BIGINT AUTO_INCREMENT PRIMARY KEY,
13+
name VARCHAR(255),
14+
position VARCHAR(16),
15+
team VARCHAR(255),
16+
fpl_id BIGINT,
17+
value DOUBLE PRECISION,
18+
total_points INT,
19+
weekly_points INT
20+
);
21+
22+
CREATE TABLE IF NOT EXISTS team (
23+
id BIGINT AUTO_INCREMENT PRIMARY KEY,
24+
name VARCHAR(255),
25+
user_id BIGINT,
26+
CONSTRAINT fk_team_user FOREIGN KEY (user_id) REFERENCES "user"(id)
27+
);
28+
29+
CREATE TABLE IF NOT EXISTS team_players (
30+
team_id BIGINT,
31+
player_id BIGINT,
32+
PRIMARY KEY (team_id, player_id),
33+
CONSTRAINT fk_tp_team FOREIGN KEY (team_id) REFERENCES team(id),
34+
CONSTRAINT fk_tp_player FOREIGN KEY (player_id) REFERENCES player(id)
35+
);
36+

0 commit comments

Comments
 (0)