Again writing fast & maintainable integration tests using in-memory db, liquibase, gradle and spring mvc test

This blog is improvisation to previously blogged testing effectively with database blog. In this blog we will remove the jetty setup and instead use Spring MVC Test framework which kind of simulate the request/response for your controllers via Spring Web Application context (no deployment, to http client tests) Here, we will write Spring MVC tests and -
  • Create the HSQLDB in-memory instance (instantiating datasource instance)
  • and run Liquibase scripts against them. (using Spring Lqiuibase bean)
1. Setup your web-service.

Write usual Controller->Service->Repository layer.

public class MapAnomalyReportEndpoint {

    private MapAnomalyReportService reportService;

    @RequestMapping(method = RequestMethod.GET)
    public Collection getReports() {
        return reportService.getReports();

2. Create Spring Java Config (No XML)

Configure the controllers.

@ComponentScan(basePackages = "com.rohankar.playground")
public class WebConfig {

    public InternalResourceViewResolver viewResolver() {
        final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        return resolver;

Configure the data source. I'm taking JDBC parameters from context, you can very well read them from .properties or system prop.
public class JdbcConfig {

    private Environment env;

    @Bean(destroyMethod = "close")
    public DataSource getDataSource() throws NamingException {
        final Context ctx = new InitialContext();
        final BasicDataSource ds = new BasicDataSource();
        return ds;

    public JdbcTemplate getJdbcTemplate() throws NamingException {
        return new JdbcTemplate(getDataSource());

3. Create Spring Java Config for tests

This config is purely for running liquibase scripts on datasource.

public class TestConfig {

    private DataSource dataSource;

    public SpringLiquibase getLiquibase() {
        final SpringLiquibase liquibase = new SpringLiquibase();
        return liquibase;
4. Write integration test In setUp method, we're populating a context object with in-memory db parameters.
@ContextConfiguration(classes = {TestConfig.class})
public class MapAnomalyReportEndpointIntTest {

    private WebApplicationContext context;

    public static void setup() throws IllegalStateException, NamingException {
        final SimpleNamingContextBuilder context = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
        context.bind("java:comp/env/report/db/driverClass", "org.hsqldb.jdbc.JDBCDriver");
        context.bind("java:comp/env/report/db/url", "jdbc:hsqldb:mem:reportdb;shutdown=false");
        context.bind("java:comp/env/report/db/user", "sa");
        context.bind("java:comp/env/report/db/password", "");
        context.bind("java:comp/env/report/db/schema", "report");

    public void testGetReports() throws Exception {
        MockMvcBuilders.webAppContextSetup(context).build() //
            .perform(get("/reports")) //
            .andDo(print()) //
            .andExpect(status().isOk()) //
            .andExpect(content().contentType("application/json;charset=UTF-8")) //
            .andExpect(jsonPath("$[0].id").value(1)); // TODO assert other values
5. Run the tests, this wont require you to configure jetty, deploy the ws and run test against them!

You can clone the whole project from here.

PS: The given project is built using Gradle as I want to learn it.


Popular Posts