Use MariaDB in tests instead of HSQLDB
Utilizes Testcontainers to start a MariaDB Docker container to use in tests. The empty database is then migrated using Flyway which means migrations are implicitly tested as well.
This commit is contained in:
parent
1554d4bc27
commit
b245051ccc
core
pom.xml
19
core/pom.xml
19
core/pom.xml
@ -86,8 +86,23 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>mariadb</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mariadb.jdbc</groupId>
|
||||
<artifactId>mariadb-java-client</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-mysql</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
@ -4,9 +4,6 @@
|
||||
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
|
||||
version="3.0">
|
||||
|
||||
<!-- NOTE THAT THERE ARE TWO PERSISTENCE UNITS, one default and one test
|
||||
used for either running or unit-tests -->
|
||||
|
||||
<!-- A JPA Persistence Unit -->
|
||||
<persistence-unit name="defaultPersistenceUnit"
|
||||
transaction-type="RESOURCE_LOCAL">
|
||||
@ -17,13 +14,4 @@
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<!-- A JPA Persistence Unit used for tests -->
|
||||
<persistence-unit name="testPersistenceUnit"
|
||||
transaction-type="RESOURCE_LOCAL">
|
||||
<properties>
|
||||
<property name="jakarta.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver"/>
|
||||
<property name="jakarta.persistence.jdbc.url" value="jdbc:hsqldb:mem:test"/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="create"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
|
@ -4,28 +4,47 @@ import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
import jakarta.persistence.Persistence;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Clock;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.mariadb.jdbc.MariaDbDataSource;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.testcontainers.containers.MariaDBContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import se.su.dsv.scipro.CoreConfig;
|
||||
import se.su.dsv.scipro.RepositoryConfiguration;
|
||||
import se.su.dsv.scipro.profiles.CurrentProfile;
|
||||
import se.su.dsv.scipro.sukat.Sukat;
|
||||
import se.su.dsv.scipro.system.CurrentUser;
|
||||
|
||||
@Testcontainers
|
||||
public abstract class SpringTest {
|
||||
|
||||
private EntityManager entityManager;
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
|
||||
@Container
|
||||
static MariaDBContainer<?> mariaDBContainer = new MariaDBContainer<>("mariadb:10.11");
|
||||
|
||||
@BeforeEach
|
||||
public final void prepareSpring() {
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory("testPersistenceUnit");
|
||||
public final void prepareSpring() throws SQLException {
|
||||
MariaDbDataSource dataSource = new MariaDbDataSource(mariaDBContainer.getJdbcUrl());
|
||||
dataSource.setUser(mariaDBContainer.getUsername());
|
||||
dataSource.setPassword(mariaDBContainer.getPassword());
|
||||
|
||||
Flyway.configure().dataSource(dataSource).load().migrate();
|
||||
|
||||
Map<String, Object> jpaProperties = Map.of(Environment.JAKARTA_JTA_DATASOURCE, dataSource);
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory("defaultPersistenceUnit", jpaProperties);
|
||||
this.entityManager = entityManagerFactory.createEntityManager();
|
||||
EntityTransaction transaction = entityManager.getTransaction();
|
||||
transaction.begin();
|
||||
|
9
pom.xml
9
pom.xml
@ -34,7 +34,6 @@
|
||||
<querydsl.version>5.0.0</querydsl.version>
|
||||
<jakarta.servlet.version>5.0.0</jakarta.servlet.version>
|
||||
<junit.version>5.9.3</junit.version>
|
||||
<hsqldb.version>2.7.1</hsqldb.version>
|
||||
<mockito.version>5.3.1</mockito.version>
|
||||
<flyway.version>9.19.1</flyway.version>
|
||||
<jersey.version>3.1.6</jersey.version>
|
||||
@ -133,14 +132,6 @@
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Database stuff -->
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>${hsqldb.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mariadb.jdbc</groupId>
|
||||
<artifactId>mariadb-java-client</artifactId>
|
||||
|
Loading…
x
Reference in New Issue
Block a user