Javatpoint Logo
Javatpoint Logo

Spring Boot Starter Test

The spring-boot-starter-test is the primary dependency for the test. It contains the majority of elements required for our tests.

There are several different types of tests that we can write to help test and automate the health of an application. Before starting any testing, we need to integrate the testing framework.

With Spring Boot, we need to add starter to our project, for testing we only need to add the spring-boot-starter-test dependency.

It pulls all the dependencies related to test. After adding it, we can build up a simple unit test. We can either create the Spring Boot project through IDE or generate it using Spring Initializr.

Note: If you are adding test dependency manually, add it to the bottom of the pom.xml file.

In the above dependency, one thing to be noticed that it includes the scope of test <scope>test</scope>. It means when the application is bundled and packaged for deployment, any dependency that is declared with the test scopes is ignored. The test scope dependencies are only available when running in the development and Maven test modes.

When we create a simple Spring Boot application, by default, it contains the test dependency in the pom.xml file and file under in the folder src/test/java.

Let's create a simple maven project.

Spring Boot Starter Test Example

Step 1: Open Spring Initializr

Step 2: Provide the Group name and Artifact Id. We have provided Group name com.javatpoint and Artifact spring-boot-test-example.

Step 3: Add the Spring Web dependency.

Spring Boot Starter Test

Step 4: Click on the Generate button. When we click on the Generate button, it wraps all the specifications related to the project and downloads a Jar file to our local system.

Step 5: Extract the downloaded Jar file.

Step 6: Import the folder to STS. It takes some time to import.

File -> Import -> Existing Maven Projects -> Browse -> Select the folder spring-boot-test-example -> Finish

After importing the project, we can see the following project directory in the Package Explorer section of the STS.

Spring Boot Starter Test

We can see in the above directory that it contains a test file named in the folder src/test/java.

The above code implements two annotation by default: @SpringBootTest, and @Test.

  • @SpringBootTest: It applies on a Test Class that runs Spring Boot based tests. It provides the following features over and above the regular Spring TestContext Framework:
    • It uses SpringBootContextLoader as the default ContextLoader if no specific @ContextConfiguration(loader=...) is defined.
    • It automatically searches for a @SpringBootConfiguration when nested @Configuartion is not used, and no explicit classes are specified.
    • It provides support for different WebEnvironment modes.
    • It registers a TestRestTemplate or WebTestClient bean for use in web tests that are using the webserver.
    • It allows application arguments to be defined using the args attribute.

Step 7: Open the file and run it as Junit Test.

Spring Boot Starter Test

When we run the above code, it displays the following:

Spring Boot Starter Test

Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA