Level 7 - Milestone 12

Milestone 12 - Preparing for Integration Testing

In this section we will edit our project to prepare for integration testing.

Before completion of this milestone, students will:

  • Learn the difference between unit testing and integration testing
  • Edit the build.gradle file to support our integration tests.

What is Integration Testing?

When unit testing our code, we wanted to ensure that individual units of code worked as expected. We used an instance of an object to invoke a specific method, and then verified that the value that the method returned was equal to the value that we expected. On a broad level, integration testing aims to verify that different units of code integrate together as expected. While some sources consider end-to-end tests of applications to qualify as integration tests, we will focus on much narrower integration tests. More specifically, we will test that the code that we wrote integrates with the code that Spring provided us.

Updating the build.gradle File

There is some code that we need to add to our build.gradle file in order to facilitate the running our upcoming integration tests. While our project came with a "test" source set where we could place our unit tests, we will add support for an "intTest" package where our integration tests will live. The following sections of code can be added back-to-back at the end of the build.grade file, but they will be broken up here for the sake of providing an explanation as to what the purpose of each section is:

sourceSets {
	intTest {
		compileClasspath += sourceSets.main.output
		runtimeClasspath += sourceSets.main.output

This defines a new source set, "intTest", for our integration tests. A source set is simply a logical grouping of source files. Before this step, our project already had two source sets: "main" and "test".
configurations {
	intTestImplementation.extendsFrom testImplementation
	intTestRunTimeOnly.extendsFrom runtimeOnly

The first line attaches all of the unit test dependencies to the integration tests. The second line creates an "intTestRunTimeOnly" configuration and attaches any dependencies that are only required at runtime.
task intTest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'

    testClassesDirs = sourceSets.intTest.output.classesDirs
    classpath = sourceSets.intTest.runtimeClasspath
    shouldRunAfter test

This section creates a new gradle task for our intTests. You can easily view all of the available gradle tasks in IntelliJ by opening the gradle menu along the right side of the IDE window. Under Tasks>Verification you should see that there are currently "test" and "check" tasks. This section will add another task there called "intTest" that will run our integration tests.
check.dependsOn intTest

Finally, this short section will cause our "intTest" task to be executed when the "check" task is run. This means that the "check" task will run both our unit tests and our integration tests.

Adding the Packages

Next, lets add the packages for our integration tests. These will have the same structure as our "main" and "test" source sets, but since we will only be writing integration tests for our presentation package, will contain only a subset of those source set files. We will add an "intTest" package at the same level as the the "main" and "test" packages, as well as the corresponding "java.org.jointheleague.level7.cheetah.presentation" packages inside of the "intTest" package. You can copy and paste those packages from one of the other source sets to make it easier.

Summary of Code Changes for this Milestone

    • src
      • intTest
        • java
          • org.jointheleague.level7.cheetah
            • presentation
      • main
        • java
          • org.jointheleague.level7.cheetah
            • config
              • ApiDocConfig.java
            • presentation
              • HomeController.java
              • LocController.java
            • service
              • LocService.java
            • repository
              • dto
                • Result.java
                • LocResponse.java
              • LocRepository.java
          • resources
            • application.yml
      • test
        • java
          • org.jointheleague.level7.cheetah
            • presentation
              • HomeControllerTest.java
              • LocControllerTest.java
            • service
              • LocServiceTest.java
            • repository
              • LocRepositoryTest.java
    • build.gradle