Skip to content

nielsutrecht/spring-datastore-examples

Repository files navigation

Spring Data Store Integration Test Examples

This project contains reference implementations demonstrating how to integrate with data stores and queues/topics and how to then implement integration tests with them. I created this project mainly because there are often recurring patterns at my clients and I am able to reuse these approaches between them.

Most of the integration tests use the excellent Testcontainers library to spin up docker containers during tests.

The reference implementations implement BaseDbIntegrationTest in the case of data stores, BaseCacheIntegrationTest in the case of caches and/or BaseTopicIntegrationTest in the case of topics / queues. This saves me a lot of time by not having to rewrite the same tests for all the different modules :)

The Hazelcast tests are a good example of how the Base tests are reused.

Implemented

Notes

In general, it's important to remember that while most specific testcontainers (Cassandra, Postgres, etc.) expose the correct port by default, the generic container doesn't do this. So if you need to use a GenericContainer, make sure you don't forget the .withExposedPorts(<port>).

I personally prefer creating reusable context initializers over @Container annotations in my tests because it allows me to set any Spring config property before Spring itself starts. Another big benefit is that it allows me to not use Testcontainers at all when I'm running on a CI system like Gitlab, and instead connect to a Gitlab service.

But @Container annotations work just as well in most other cases.

Infinispan

Infinispan was a bit more complex to set up than most others. A lot of documentation is for much older versions. In addition, the Infinispan documentation forgets to mention that you still need to add the Spring Boot caching starter. You also have to programmatically create the caches. So if you see a message like this:

Error received from the server: org.infinispan.server.hotrod.CacheNotFoundException: 
    Cache with name 'yourcache' not found amongst the configured caches

or:

java.lang.IllegalArgumentException: Cannot find cache named 'yourcache' for Builder[..]

It's because you simply did not create one:

    var cfg = new ConfigurationBuilder()
            .clustering()
            .cacheMode(CacheMode.LOCAL)
            .build();

    cacheManager.administration().createCache("product", cfg);

Make sure you don't set CacheMode to something like DIST_SYNC because then it will try to connect a cluster, which it won't be able to, and then time out.

Neo4J

The Neo4J 4.3 docker image seems to not listen properly on whatever Testcontainers is waiting on. 4.4 works fine :)

Also the Spring Boot guide is unfortunately rather outdated.

DynamoDB

Make sure you use the AWS v2 SDK. I had a lot of problems simply because I followed the first guide I ran into, which happened to be a V1 guide. This one has a problem with newer Java versions due to how it uses reflection. So; just use the V2 version of the API and you're fine.

This implementation creates the Product table in the config. How you want to do that is up to you :)

Documentation:

There are some unofficial Spring Data implementations for DynamoDB but every one I found seems to be dead, or at least not up to date and using the V2 API.

About

Examples on how to integrate and test different databases/datastores with Spring and TestContainers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published