Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Requirements:

The test scope should be end-to-end: starting from resource and going through all application to the h2 database.

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). Another issue is those should be rolled back after each test, use transaction support ( currently @Transactional annotation does nothing to help). What's the minimal set of Spring's tools required to support this kind of tests? How to make it work? Should @Transactional be placed anywhere else?

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:

Failsafe Plugin documentation suggests binding starting of container to pre-integration-test phase and post-integration-test at the container termination. How can it be configured if my jersey-container-grizzly2-http container is configured programmatically? 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.

like image 856
J.Olufsen Avatar asked Apr 30 '16 23:04

J.Olufsen


1 Answers

I think your scenario is a great fit for dbunit testing. 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. The data model of this database entirely depends on the dataset xml you configure. Have a look at this: http://archive.oreilly.com/pub/post/dbunit_made_easy.html

like image 151
ritesh.garg Avatar answered Nov 08 '22 09:11

ritesh.garg