Skip to content

Instantly share code, notes, and snippets.

@rkbalgi
Last active November 30, 2018 15:42
Show Gist options
  • Save rkbalgi/0bec02e7cefc89e044dd90b71d624e40 to your computer and use it in GitHub Desktop.
Save rkbalgi/0bec02e7cefc89e044dd90b71d624e40 to your computer and use it in GitHub Desktop.
Creating multiple databases before test execution (Junit, SpringBoot)
Sometimes there is a need to create multiple databases prior to running integration tests (in a typical Spring/Spring Boot project). We can achieve this using a BeforeClass method in a JUnit test suite. Attached in this gist is the JUnit test suite and a persistence.xml file.
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_3_0.xsd"
version="2.0">
<persistence-unit name="tenant" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.example.demoapp.entities.Tenant</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="packagesToScan" value="com.example.demoapp.entities"/>
</properties>
</persistence-unit>
</persistence>
package com.example;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.h2.tools.Server;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
*
*/
@RunWith(Suite.class)
@SuiteClasses({TenantTest.class, AnotherTenantTest.class})
public class SampleTestSuite {
public static final int NO_OF_TENANTS = 10;
/**
* Create multiple databases before running the applications and tests
*/
@BeforeClass
public static void createDatabases() throws InterruptedException, SQLException {
//delete all the existing databases (this can also be done in a method with @AfterClass)
//this is not necessary when running in-memory database
Stream.of(Paths.get(System.getenv("USERPROFILE")).toFile().list((dir, fileName) -> {
if (fileName != null && fileName.startsWith("tenant") && fileName.endsWith(".db")) {
return true;
}
return false;
})).forEach(file -> {
boolean result = Paths.get(System.getenv("USERPROFILE"), file).toFile().delete();
System.out.println("file - " + file + (result ? " deleted" : " not deleted"));
});
Server server = Server.createTcpServer("-tcpPort", "9123", "-tcpAllowOthers").start();
while (!server.isRunning(true)) {
}
HibernatePersistenceProvider provider = new HibernatePersistenceProvider();
Map<String, String> props = new HashMap<>();
for (int i = 0; i < NO_OF_TENANTS; i++) {
System.out.println("Configuring database for ... Tenant-" + i);
props.put("javax.persistence.jdbc.url", "jdbc:h2:tcp://localhost:9123/~/tenant" + i);
//props.put("javax.persistence.jdbc.username", "sa");
//props.put("javax.persistence.jdbc.password", "sa");
//some tenants can have default data
if (i == 0) {
props.put("javax.persistence.sql-load-script-source", "META-INF/tenant0_data.sql");
} else if (i == 5) {
props.put("javax.persistence.sql-load-script-source", "META-INF/tenant5_data.sql");
} else {
props.remove("javax.persistence.sql-load-script-source");
}
//this will generate all the tables from defined models
provider.generateSchema("tenant", props);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment