Table of contents
1.
Introduction
2.
Spring Boot Starters
3.
Spring Boot Starter Test
4.
FAQs
5.
Key Takeaways
Last Updated: Oct 24, 2024

Spring Boot Starter Test

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Spring Boot Starters are the dependency descriptor that can be included in the project. Spring Boot provides several Starters that can be used to add all the dependencies required for a project.

Spring Boot Starter Test is one of the starters provided by Spring Boot.

Let’s understand the Spring Boot Starters with examples.

Spring Boot Starters

As we already discussed in the introduction section, Spring Boot Starters are the dependency descriptor included in the project.

Spring Boot Starters can be added to the pom.xml file of our project as per the project's requirement. You need to add the starters to the pom.xml file, and those starters will automatically provide all the required dependencies.

For example, if we want to create a web application, we need to add spring-boot-starter-web in the pom.xml file. This spring-boot-starter-web itself contains a pom.xml in which all the required dependencies for a web application project are present.

The below starter for spring-boot-starter-web can be added to pom.xml to create a web applications:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
You can also try this code with Online Java Compiler
Run Code

 

Only the above starter is required to add to the pom.xml file, and this starter will automatically provide all the web-related dependencies.

Spring Boot provides several Starters that can be used to add all the dependencies required for a project. Let’s now discuss the spring-boot-starter-test in depth.

You can also consider our Spring Boot Course to give your career an edge over others.

Spring Boot Starter Test

Spring Boot Starter Test is used for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest, and Mockito. It is the primary dependency for the test. 

For testing, we need to add the spring-boot-starter-test dependency into the pom.xml file. 

When we create a spring boot application the spring-boot-starter-test is by default added into pom.xml.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
You can also try this code with Online Java Compiler
Run Code

 

Note: If the test dependency is added manually, it should be added to the bottom of the dependencies tag in the pom.xml.

You can see the above code in the following picture:

 

We can notice in the above dependency 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 mode.

Whenever we create a spring boot application, the spring-boot-starter-test is by default added into the pom.xml and ApplicationTests.java file under the folder src/test/java.

You can see that the src/test/java folder contains ApplicationTests.java (CodingNinjasApplicationTests.java here)  in the following picture:

 

To run a test on your application, open the ApplicationTest.java file inside the src/test/java folder and run it as Junit Test. Like below.


After running the test file as Junit Test, we get the following output:

It shows that the particular unit has no error (Errors: 0), and the application runs successfully without any interruption or failure traces(Failure: 0).

FAQs

  1. What is the spring boot starter test?
    Spring Boot Starter Test is used for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest, and Mockito. It is the primary dependency for the test.
     
  2. Do we need to add the spring-boot-starter-test dependency into the pom.xml file manually?
    No, the spring-boot-starter-test dependency is by default present in the pom.xml of our application.
     
  3. Does the spring-boot-starter-test dependency is required in production?
    The unit testing is only to check if each unit of our application is working properly or not. The spring-boot-starter-test dependency is not required during the deployment stage. That’s why they are declared with the test scopes. The test scope dependencies are only available when running in the development mode and excluded during the production stage.

Key Takeaways

In this blog, we have learned about the spring boot starter test, how to run a unit test in a spring boot application.

Happy Learning!!

Live masterclass