How to integrate setUp () tests to manipulate HSQL data in the internal memory database in the built-in Jetty container?

I am trying to run integration tests regarding a REST web service process that runs in an embedded berth container during the maven integration integration phase. It works.

I want to configure the server to use the HSQL database in memory, so that every JUnit test can configure the database (create tables, insert records) and tear it (delete records).

The web service process application context defines the following data source:

<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 doing unit tests (which did not rely on Jetty's built-in container to run), this setting worked fine. Each unit test created a database and inserted records as follows:

 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 does not work, because the data source created when I started my server in Jetty is not available for my unit test class for inserting / deleting data.

My question is:

  • How to configure HSQL in an embedded Jetty container so that my unit test setUp () method can manage data?
+4
source share
1 answer

Publish my own solution here if it is useful along the way to someone else.

Good, so I did not solve it the way I hoped.

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

Therefore, instead of solving my problem this way, the server itself simply loaded data at startup. In src / test, I added a DB initialization servlet that would run the HSQL database in memory and then execute the insert statements to load the test data. Then I copied web.xml from src / main / webapp to src / test / webapp (didn’t want to do this) and added this test servlet to load at startup.

Thus, the tests themselves do not actually insert data between the tests, but they call the doget () method on my new test servlet to tell it to update the database in memory.

0
source

Source: https://habr.com/ru/post/1390331/


All Articles