Creating a strong foundation for a new project using Spring Boot – Part 4

Contents

This post is divided into four parts.

  1. Getting Ready
  2. Unit testing
  3. Integration testing
  4. Launching the app

Launching the app

At this point, our application is complete. You can launch the app by executing the following command.

[code language=”plain”]

gradlew bootRun

[/code]

Make sure you have modified your application.properties according to your DB and that your DB is up and running.

Try our some GET/POST requests. You can use CURL or a GUI based chrome extension Postman. There are similar tools available for other platforms. Make sure to query your DB to check the modifications.

Adding the ability to run anywhere

Making your application flexible enough to be able to run on anywhere could be very useful. Particularly when you have to demonstrate.

Copy the file application-integrationtest.properties to src/main/resources and rename it to application-testrun.properties. The contents of the file will not change as our application is very small and we do not have separate properties to be configured.

The file application-testrun.properties will be automatically picked up by Spring when testrun profile is active.

In order to launch our application in testrun mode, add the following to your build.gradle.

[code language=”plain”]
task testRun(type: org.springframework.boot.gradle.run.BootRunTask, dependsOn: ‘build’) {
group = ‘Application’
doFirst() {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
args = ["–spring.profiles.active=testrun"]
}
}
[/code]

Here, we have created a separate task to testRun (notice the uppercase R, this is as per Gradle conventions) to activate the testrun profile. Having a separate gradle task activate a property will make it easy for anyone to launch the app with minimal efforts.

Starting the testRun

As you might have guessed it, execute the following command.

[code language=”plain”]

gradlew testRun

[/code]

Stop your DB and perform similar requests as you did previously. Notice that everything works smooth. Only the difference is that the data will be lost upon the restart as we are using H2.

Get the complete source code here.

Read another post related to the assertj library.

Hope you enjoyed the post.