A micro framework to make it easier to perform offline / local integration testing when using Spring and Hibernate within a Java backend.
By providing the ability to simplify swapping out of the JPA based persistence configuration, a Data Access Layer and its objects can be tested quickly and easily to ensure that criteria and any other SQL/JPQL/HQL logic is performing as expected. This is achieved by utilising an in-memory database in order to run integration tests through a lightweight, low footprint, local database.
By default an in-memory H2 database instance is used but the configuration can easily be adjusted to fit requirements.
Implement the OfflineIntegrationSupport interface, extend the provided instance of OfflineIntegrationConfiguration and provide any persistence adjustments (including overriding getEntityPackagesToScan) or bean references you need.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class MyIntegrationTest implements OfflineIntegrationSupport {
@Configuration
public static MyIntegrationTestConfiguration extends OfflineIntegrationConfiguration {
@Override
public String[] getEntityPackagesToScan() {
return new String[] {"com.mypackage.model"};
}
....
}
}
Override getEntityPackagesToScan
to provide an array of package names to be scanned for annotated Entity subclasses.
@Override
public String[] getEntityPackagesToScan() {
return new String[] {"com.mypackage.model", "com.thirdpartylib.models"};
}
Override getJPAProperties
if you want to adjust any JPA EntityManagerFactory / Hibernate SessionFactory properties.
// To make sure you can see the SQL being created and used (this is off by default).
@Override
protected Properties getJPAProperties() {
Properties properties = super.getJPAProperties();
properties.setProperty(HibernateShowSQLPropertyName, "true");
return properties;
}
Override getDataSourceUsername
to specifiy your own username. (This is blank by default).
@Override
public String getDataSourceUsername() {
return "myusername";
}
Override getDataSourcePassword
to specifiy your own password. (This is blank by default).
@Override
public String getDataSourcePassword() {
return "mypassword";
}
Override getDataSourceURL()
to change the location and/or type of database used. (This is 'jdbc:h2:mem:test;DB_CLOSE_DELAY=-1' by default).
@Override
public String getDataSourceURL() {
// Create the test database on the filesystem in the users home directory
return "jdbc:h2:~/test";
}
Override getDataSourceDriverClassName
, getDialectPropertyClassName
and getDataSourceURL
to configure the connection to the database.
// switch to local MySQL database for integration testing
@Override
public String getDataSourceDriverClassName() {
return "com.mysql.jdbc.Driver";
}
@Override
public String getDialectPropertyClassName() {
return "org.hibernate.dialect.MySQL5Dialect";
}
@Override
public String getDataSourceURL() {
return "jdbc:mysql://localhost:3306/integration_testing";
}
A full example of utilising this framework both for testing and as a basis to configure a spring-based app is provided in the this repository.