4

I am trying to run integration tests against a REST web service process that is started in an embedded jetty container within the maven integration test phase. That much is working.

I want to configure the server to use an in-memory HSQL DB such that each JUnit test can setup the database (create tables, insert records), and tear it down (delete the records).

The application context of the web services process defines the following datasource:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="#{applicationProperties['jdbc.driver.class.name']}" />
    <property name="url" value="#{applicationProperties['jdbc.url']}" />
    <property name="username" value="#{applicationProperties['db.user']}" />
    <property name="password" value="#{applicationProperties['db.pass']}" />
</bean>

Properties:

jdbc.driver.class.name=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:mem:mytestdb
db.user=sa
db.pass=

When executing unit tests (that did not rely on the embedded Jetty container to be running), this setup worked fine. Each unit test created the database and inserted the records like so:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
public class TestBase {

    @Autowired
    protected ApplicationContext context;

    ...

    @Before
    public void setUp() {
        DriverManagerDataSource ds = (DriverManagerDataSource) context.getBean("dataSource");
        // Create tables
        // Insert records
    }
}

With my integration tests, this is not working -apparently because the datasource that is created when my server is started in Jetty is not accessible by my unit test class to insert/delete the data.

My question:

  • How can I configure HSQL in my embedded Jetty container so that my unit test setUp() method can manipulate the data?
fredt
  • 24,044
  • 3
  • 40
  • 61
gregdferrell
  • 290
  • 3
  • 11

1 Answers1

0

Posting my own solution here in case it's useful down the road to someone else.

Allright, so I didn't end up solving this the way I had hoped.

I could not find a way to have my integration tests insert data into the in-memory HSQL database that was running on the server.

So instead of solving my problem this way, I had the server itself just load the data on startup. Under src/test, I added a DB initialization servlet that would start the in-memory HSQL DB, then execute the insert statements to load the test data. I then copied the web.xml from src/main/webapp to src/test/webapp (didn't like having to do this), and added this test servlet to load on startup.

So, the tests themselves don't actually insert the data in between tests, but they call a doget() method on my new test servlet to tell it to refresh the in-memory database.

gregdferrell
  • 290
  • 3
  • 11