In this blog post, I will give you an overview of Spring Boot’s capabilities to test the data layer of your application.
There is a vast amount of technologies to choose from when it comes to loading and persisting data. There are not only completely different types of data stores but also different ways to communicate with them. Which combination is the most fitting, depends on your application. Whatever you choose, you should consider testing the components communicating with your data store, to prevent working with the wrong data.
The @Data*Test
annotations of Spring Boot offer a great way to test these components in isolation while providing some handy niceties to make your life easier.
What is the Data Layer
In general, a layer is a logical group of components that serve the same technical purpose. In the classical three-layer architecture, the three layers – presentation, business and data – are stacked on top of each other. The lower-level layers hide their implementation details and complexity by providing services to the upper-level layers.
The data layer is responsible for reading and writing data from and to other systems. Most of the time this is a database, but it can also be another application. The business layer contains the business logic and uses the services provided by the data layer. Because it is crucial that the business logic works as expected, it gets tested. However, it is as important that this business logic is applied to the correct data. This is the reason why you should test your data layer. Queries can become quite complex and deserve testing. If data is loaded from different sources and needs to be merged, this can be tested too. Sometimes your data layer needs to transform and validate data, which can also be tested.
Supported Technologies
There is a wide range of technologies when it comes to persisting data. Luckily, Spring Boot offers convenient ways to test communication with the more common ones. The @Data*Test
s are a collection of annotations that disable the full auto-configuration of your application and instead configure only the subset of components that are used as a part of the data layer.
Below is a list of supported technologies and a brief description of what annotation to use.
JPA
To test your JPA components, you can annotate your Tests with @DataJpaTest
.
All tests with this annotation will disable the full auto-configuration and instead will only configure JPA components. By default, these tests will use an embedded in-memory database. If you rather want to use a real instance of your database, you can configure it with the @AutoConfigureTestDatabase
annotation. These tests are also transactional and will roll back at the end of each test.
For more detailed information have a look at ‘How to test the Data Layer of your Spring Boot Application with @DataJpaTest’.
JDBC
If your application communicates with your database via JDBC, you can use the @DataJdbcTest
annotation to test your JDBC components. As before – and like with all following annotations – the full auto-configuration is disabled in tests with this annotation. Only JDBC components will be configured. By default, these tests will also use an embedded in-memory database and have the same transactional behavior as the @DataJpaTest
annotated tests.
jOOQ
If you use jOOQ as your database abstraction, you can test your responsible components by annotating your tests with @JooqTest
. Unlike the other ones, tests annotated with @JooqTest
will use the configured database instead of an embedded in-memory one. To use an in-memory database, you can add the @AutoConfigureTestDatabase
annotation.
MongoDB
Your application works with documents and persists them in a MongoDB? Spring Boot has you covered! Test your MongoDB components by annotating your test with @DataMongoTest
. Those tests will use an embedded in-memory MongoDB process by default if it is available. Otherwise, the tests will use the configured MongoDB.
Neo4J
You use Neo4J to persist your data and its relationships in a graph? Annotate your Tests with @DataNeo4jTest
and test your components. The tests will use the configured external Neo4J Service. Like with the other transactional databases, the tests are transactional by default and will roll back at the end.
Redis
If your application stores key-value pairs in a Redis database, you can use the @DataRedisTest
annotation on your tests to verify the functionality of your components. Tests with this annotation will use the configured Redis database.
LDAP
Spring Boot also supports the testing of components that handle data over LDAP. Your @DataLdapTest
annotated tests will use an in-memory directory server if it is available. Otherwise, they will connect to the configured one.
Cassandra
If your application uses Cassandra to handle data, you can use the @DataCassandraTest
annotation on your tests to verify your components. The tests will connect to the configured Cassandra database.
R2DBC
With the @DataR2dbcTest
annotation, Spring Boot offers a way to test your components that use R2DBC to query data asynchronously from your data store. The tests will use the configured database.
Resources
- Interested in more information about
@DataJpaTest
? Have a look at “How to test the Data Layer of your Spring Boot Application with @DataJpaTest’” - You need information about
@DataMongoTest
? Have a look at “How to test the Data Layer of your Spring Boot Application with @DataMongoTest’”