简体   繁体   English

如何进行集成测试setUp()操作在嵌入式Jetty容器中运行的内存数据库中的HSQL数据?

[英]How to have integration tests setUp() manipulate HSQL data in an in-memory DB running in embedded Jetty container?

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. 我正在尝试针对在maven集成测试阶段的嵌入式jetty容器中启动的REST Web服务进程运行集成测试。 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). 我想配置服务器以使用内存中的HSQL DB,这样每个JUnit测试都可以设置数据库(创建表,插入记录),并将其拆除(删除记录)。

The application context of the web services process defines the following datasource: Web服务进程的应用程序上下文定义了以下数据源:

<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. 执行单元测试(不依赖于嵌入式Jetty容器运行)时,此设置工作正常。 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. 使用我的集成测试,这是行不通的 - 因为我的服务器在Jetty中启动时创建的数据源不能被我的单元测试类访问以插入/删除数据。

My question: 我的问题:

  • How can I configure HSQL in my embedded Jetty container so that my unit test setUp() method can manipulate the data? 如何在我的嵌入式Jetty容器中配置HSQL,以便我的单元测试setUp()方法可以操作数据?

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. 我找不到让集成测试将数据插入服务器上运行的内存中HSQL数据库的方法。

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. 在src / test下,我添加了一个DB初始化servlet,它将启动内存中的HSQL DB,然后执行insert语句来加载测试数据。 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. 然后我将web.xml从src / main / webapp复制到src / test / webapp(不喜欢这样做),并添加了这个测试servlet以在启动时加载。

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. 因此,测试本身实际上并没有在测试之间插入数据,但是他们在我的新测试servlet上调用doget()方法来告诉它刷新内存数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM