简体   繁体   English

Jersey e2e使用Spring的JDBCTemplate对内存数据库进行集成测试

[英]Jersey e2e integration tests for in-memory database using Spring's JDBCTemplate

What is the proper way of designing and running e2e integration tests for Jersey REST API that uses jersey-test-framework-provider-inmemory , h2 database and org.springframework.jdbc.core.JdbcTemplate ? 设计和运行使用jersey-test-framework-provider-inmemory ,h2数据库和org.springframework.jdbc.core.JdbcTemplate Jersey REST API的e2e集成测试的正确方法是什么?

Requirements: 要求:

The test scope should be end-to-end: starting from resource and going through all application to the h2 database . 测试范围应该是端到端的:从资源开始并通过所有应用程序到h2数据库

Writing tests: 写作测试:

Currently my JUnit integration tests fail if run all together from IDE's JUnit, mainly because they interfere with each other(running concurrently with JUnit). 目前,如果从IDE的JUnit中一起运行,我的JUnit集成测试将失败,主要是因为它们相互干扰(与JUnit并发运行)。 Another issue is those should be rolled back after each test, use transaction support ( currently @Transactional annotation does nothing to help). 另一个问题是那些应该在每次测试后回滚,使用事务支持(目前@Transactional注释没有任何帮助)。 What's the minimal set of Spring's tools required to support this kind of tests? 支持这种测试所需的最小Spring工具集是什么? How to make it work? 如何使它工作? Should @Transactional be placed anywhere else? @Transactional应该放在其他地方吗?

Example: 例:

@Transactional
public class OrderResourceIT extends JerseyTest {

    @Override
    protected Application configure() {
        // config
    }

    @Override
    protected void configureClient(final ClientConfig config) {
        // config
    }

    @Before
    public void startUp(){
        // database bootstrap programmatically before each test
    }


    @Test
    @Transactional
    public void testGetOrders(){
        Response response  = target("/orders")
                .request()
                .get();

        List<Order> orders = response.readEntity(new GenericType<List<Order>>(){});

        // asserts
    }
}

Execution: 执行:

It's planned to execute with maven-failsafe-plugin : 它计划用maven-failsafe-plugin执行:

Failsafe Plugin documentation suggests binding starting of container to pre-integration-test phase and post-integration-test at the container termination. Failsafe插件文档建议在容器终止时将容器绑定到pre-integration-test phasepost-integration-test pre-integration-test phase How can it be configured if my jersey-container-grizzly2-http container is configured programmatically? 如果我的jersey-container-grizzly2-http容器是以编程方式配置的,如何配置? Example : 示例

public class Main {
    // Base URI the Grizzly HTTP server will listen on
    public static final String BASE_URI = "http://localhost:8080/myapp/";

    public static HttpServer startServer() {
        // create a resource config that scans for JAX-RS resources and providers
        // in com.example.rest package
        final ResourceConfig rc = new ResourceConfig().packages("com.example.rest");

        // create and start a new instance of grizzly http server
        // exposing the Jersey application at BASE_URI
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    }

    public static void main(String[] args) throws IOException {
        final HttpServer server = startServer();
        System.out.println(String.format("Jersey app started with WADL available at "
                + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
        System.in.read();
        server.stop();
    }
}

Would be perfect to see working example code. 很高兴看到工作示例代码。

I think your scenario is a great fit for dbunit testing. 我认为您的场景非常适合dbunit测试。 With dbunits, an in-memory database is spawned up for every test case to be executed and is destroyed as the test case execution is complete. 使用dbunits,每个要执行的测试用例都会生成一个内存数据库,并在测试用例执行完成时销毁。 The data model of this database entirely depends on the dataset xml you configure. 此数据库的数据模型完全取决于您配置的数据集xml。 Have a look at this: http://archive.oreilly.com/pub/post/dbunit_made_easy.html 看看这个: http//archive.oreilly.com/pub/post/dbunit_made_easy.html

Hi there are ample of example provided by jersey as jersey-examples . 嗨,泽西提供了充足的例子作为球衣示例。 There are set of junit testcases for every module hosting the rest services. 托管其余服务的每个模块都有一组junit测试用例。

The example can downloaded from jersey download page . 该示例可以从泽西下载页面下载。 https://jersey.java.net/download.html https://jersey.java.net/download.html

Exact links are given below which may change in future. 下面给出了确切的链接,将来可能会有所改变。

For all available version: 对于所有可用版本:

http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jersey-examples http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jersey-examples

For current latest version: 目前的最新版本:

http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jersey-examples/2.23.1/jersey-examples-2.23.1-all.zip http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jersey-examples/2.23.1/jersey-examples-2.23.1-all.zip

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

相关问题 使用内存数据库在 Spring Boot 中进行集成测试 - Integration test in Spring Boot using in-memory database 关于使用模拟条带进行 e2e 测试 - Regarding using mock stripe for e2e tests 如何在maven中为Java项目组织单元,集成,e2e测试文件夹结构? - How to organise unit, integration, e2e tests folder structure in maven for a Java project? 使用 @DataJpaTest 的测试未使用嵌入式内存数据库 - Tests with @DataJpaTest are not using embedded in-memory database 用于集成测试的内存数据库的架构创建失败 - Schema creation for in-memory database to use in integration tests fails 内存数据库配置(HSQLDB),用于Spring启动应用程序中的集成测试 - In-memory database configuration (HSQLDB) for integration testing in spring boot app 如何使用Spring Autowiring在内存中的derby数据库中使用一堆DAO? - How Do I use a bunch of DAO's using in-memory derby database using spring autowiring.? 如何在集成测试运行时查看H2内存数据库? - How to view H2 in-memory database while integration tests are running? 使用内存数据库(例如SQLite)是否比将所有内容保留在HashMap或其他数据结构中更好? - Is it better to use an in-memory database (e.g. SQLite) than to keep everything in HashMap or other data structures? 使用量角器e2e测试来测量代码覆盖率Java - Measuring code coverage java using protractor e2e test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM