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?