Using the Same DbUnit Data Sets From Multiple Gradle Modules

After we have finished this lesson, we:

  • Know how we can publish our DbUnit data sets to a Maven repository.
  • Can use the published DbUnit data sets from another Gradle module.

Get the source code from Github

Publishing Our DbUnit Data Sets to a Maven Repository

When we want to use the same DbUnit data sets from multiple Gradle modules, the first thing that we have to do is to publish our DbUnit data sets to a Maven repository. We can publish our DbUnit data sets by following these steps:

First, we have to create a new Gradle project. Because we want to use the default directory layout of a Java project, we have to apply the Java plugin. After we have done this, our build.gradle file looks as follows:

1plugins {
2    id 'java'
3}

Second, we have to put our DbUnit data set files to the src/main/resources directory. This ensures that our DbUnit data sets are packaged into the created jar file. The following figure identifies the DbUnit data sets which are found from the src/main/resources directory of our example application:

Third, we have to ensure that we can publish our jar file to a Maven repository. We can do this by following these steps:

  1. We have to apply the Maven Publish plugin . This plugin allows us to publish build artifacts to a Maven repository.
  2. We have to configure the groupId, artifactId, and version of the published jar file.

After we have made the required changes to our build.gradle file, it looks as follows:

 1plugins {
 2    id 'java'
 3    id 'maven-publish'
 4}
 5
 6publishing {
 7    publications {
 8        maven(MavenPublication) {
 9            groupId = 'com.testwithspring.master'
10            artifactId = 'test-data-sets'
11            version = '0.1'
12
13            from components.java
14        }
15    }
16}

Fourth, we have publish our jar file to a Maven repository. Because this is an example, we will publish our jar file to our local Maven repository. We can do this by running the command: gradle clean publishToMavenLocal at command prompt.

If you are working with a real-life software project, you should publish the jar file to a private Maven repository that can be accessed only from a private network. The benefit of this approach is that you don’t have to publish the jar file to your local Maven repository every time when someone makes changes to the test data. Instead, you can simply update the version number of the correct dependency.

Additional Reading:

Next, we will find out how we can use our DbUnit data sets from another Gradle module.

Using Our DbUnit Data Sets From Another Gradle Module

When we want to use our DbUnit data sets from another Gradle module, we have to follow these steps:

First, we have to ensure that Gradle uses the local Maven repository when it resolves the dependencies of our Gradle build. We can do this by adding the following repositories block to our build.gradle file:

1repositories {
2    mavenCentral()
3    mavenLocal()
4}

Second, we have to ensure that our DbUnit data sets are found from the classpath. We can do this by adding the following dependency to the integrationTestRuntimeOnly dependency configuration:

1dependencies {
2    integrationTestRuntimeOnly(
3            'com.testwithspring.master:test-data-sets:0.1'
4    )
5}

Third, because the shared DbUnit data sets are found from the classpath, we can use them in the same way as “local” DbUnit data sets.

For example, let’s configure the test data which is used by an integration test which ensures that the TaskRepository object is working as expected when it creates a new task. After we have configured the test data, the source code of our test class looks as follows:

 1import com.github.springtestdbunit.annotation.DatabaseSetup
 2import com.github.springtestdbunit.annotation.DbUnitConfiguration
 3import com.github.springtestdbunit.dataset.ReplacementDataSetLoader
 4import com.testwithspring.master.DatabaseIntegrationTest
 5import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension
 6import org.junit.jupiter.api.DisplayName
 7import org.junit.jupiter.api.extension.ExtendWith
 8import org.springframework.beans.factory.annotation.Autowired
 9import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
10
11@DatabaseIntegrationTest
12@ExtendWith(SoftAssertionsExtension::class)
13@DatabaseSetup(value = [
14    "/com/testwithspring/master/users.xml",
15    "/com/testwithspring/master/no-tasks-and-tags.xml"
16])
17@DbUnitConfiguration(dataSetLoader = ReplacementDataSetLoader::class)
18@DisplayName("Create a new task")
19private class CreateNewTaskTest(
20        @Autowired private val jdbcTemplate: NamedParameterJdbcTemplate,
21        @Autowired private val repository: TaskRepository
22) {
23
24}

We can now publish DbUnit data sets to a Maven repository and use these data sets from another Gradle module. Let’s summarize what we learned from this lesson.

Summary

This lesson has taught us two things:

  • We can publish build artifacts to a Maven repository by using the Maven Publish plugin.
  • Because the shared DbUnit data sets are found from the classpath, we can use them in the same way as “local” DbUnit data sets.

Get the source code from Github