EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Test
 

Spring Boot Test

Updated March 28, 2023

Spring Boot Test

 

 

Introduction to Spring Boot Test

Spring boot test will provide us many annotation and utility which was supporting to test our application. Basically, it will provide by using two methods, first is the auto-configure (spring-boot-test-autoconfigure) and the second is the spring boot test (spring-boo-test). Spring boot test (spring-boo-test) method contains the core items and the auto-configure (spring-boot-test-autoconfigure) method supports the test of auto-configuration while using spring boot test auto-configure (spring-boot-test-autoconfigure) method it will import the both modules. Basically, application context is defined as spring boot applications,, it provides the spring boot test (@SpringBootTest) annotation.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is Spring Boot Test?

We can test our spring boot application by using an annotation name as @SpringBootTest, basically, it is providing a very efficient way to start our application context which we are using in a project. A unit testing using spring boot will cover the single unit where the unit is nothing but the one class it is also a cohesive class cluster. Integration testing covering the following points. Integration testing tests multiple units. It will interact between two or multiple cohesive classes of the cluster.

Integration testing covering the more layers. We can say that this could be the first case specialization and it was covering the interaction between the layer of persistence and the business layer. Integration testing uses it will be covering the application whole path. Using this test, we are sending a request to the application, after sending the request we are checking for the correct response. We have using the @SpringBootTest annotation to create the application context by using it.

We are using the following libraries with the spring boot starter test (spring-boot-starter-test).

  • JsonPath: This is nothing but the Xpath of JSON.
  • JSONassert: This is nothing but the assertion library used for JSON.
  • Mockito: This is a java mocking framework.
  • JUnit: This is a de-facto standard which was used for unit testing in java applications.
  • Spring boot test and spring test: This is the utilities which were used in spring boot applications.
  • AssertJ: This is nothing but an assertion fluent library which was used in spring boot applications.
  • Hamcrest: This is nothing but a library of the matching object which was used in spring boot applications.

We can test our application using unit testing, using unit by unit or we can test the whole application in one go. Annotations are used to load all the application contexts of spring. As compared test slice annotation will only load the beans which were required for the particular layer.

We can use the following annotation with the project as follows:

  • SpringBootTest
  • WebMvcTest
  • WebFluxTest
  • DataJpaTest
  • DataJdbcTest

Web MVC context annotation is used to set up our application context using specified components. Web flux test annotation is used to test the web flux controllers, this annotation is similar to the web MVC test. Only the difference in both annotations is configuration and annotation. Data JPA test annotation is used to test the layer of persistence. This annotation is used to configure the repositories, set up an embedded database. The spring data JDBC is another annotation of it. If we are using the java project and testing the persistence layer same time we can use DataJdbcTest annotation.

Below are the dependencies which are used to test the spring boot application in java as follows:

  • Spring boot starter test: This is the method which was used to test the java application.
  • Junit version: This is a library and its version was used to test the java application.

Project Setup

Given below are the steps which were used to set up the java project.

1. Add the maven dependencies

The first step is containing to add the dependency of maven in the current project.

Code:

<dependency> -- start of dependency section.
<groupId>org.springframework.boot</groupId> -- Start and end of groupid section.
<artifactId>spring-boot-starter-test</artifactId> -- Start and end of artifactid section.
<scope>springboottest</scope> -- -- Start and end of scope section.
<version>2.5.0</version> -- -- Start and end of version section.
</dependency> -- end of dependency section.
<dependency> -- start of dependency section.
<groupId>com.springboottest</groupId> -- -- Start and end of groupid section.
<artifactId>boottest</artifactId> -- -- Start and end of artifactid section.
<scope>springtest</scope> -- -- Start and end of scope section.
</dependency> -- end of dependency section.

Output:

Spring Boot Test

2. Add JUnit library

After adding the maven dependency, we are adding the JUnit library in our project.

Code:

<dependency> -- start of dependency section.
<groupId>org.junit.vintage</groupId> -- -- Start and end of groupId section.
<artifactId>junit-vintage-engine</artifactId> -- -- Start and end of artifactid section.
<scope>springtest</scope> -- -- Start and end of scope section.
<exclusion>
<groupId>org.hamcrest</groupId> -- -- Start and end of groupId section.
<artifactId>hamcrest-core</artifactId> -- -- Start and end of artifactid section.
</exclusion>
</dependency> -- end of dependency section.

Output:

Add JUnit library

3. Integration testing with @SpringBootTest annotation

In the below example, we are checking how to test the application using @SpringBootTest annotation.

Code:

@SpringBootApplication -- Spring boot application annotation.
@RunWith(SpringRunner.class) -- Run with annotation.
@SpringBootTest(
SpringBootTest.WebEnvironment.MOCK,
classes = Application.class)
@AutoConfigureMockMvc – Auto configure mockmvc annotation.
@TestPropertySource ( -- test property service annotation.
locations = "classpath:application-integrationtest.properties") -- Class path location.
public static void main /* Main method used with string arguments*/ (String[] args) {
SpringApplication.run (SpringboottestApplication.class, args);
}
public class SpringboottestApplication { -- class name as SpringboottestApplication
@Autowired -- Auto wired annotation
private MockMvc mock; -- Object creation of MockMvc method.
@Autowired -- Auto wired annotation
private EmployeeRepository repository; -- Object creation of EmployeeRepository.
}

Output:

Spring Boot Test 3

4. Application testing with @MockBean annotation

In the below example, we are checking how to test the application using @MockBean annotation.

Code:

public class SpringboottestApplication implements Springboottest { /* class name as SpringboottestApplication */
@Autowired -- Auto wired annotation
private SpringBootRepository SpringRepository;
@Override -- override annotation
public Springboot getSpringbootByapp(String app_name) {
return SpringRepository.findByName (app_name);
}
}

Output:

Application testing with @MockBean annotation

Recommended Articles

This is a guide to Spring Boot Test. Here we discuss the introduction and project setup – spring boot test for better understanding. You may also have a look at the following articles to learn more –

  1. Maven Repository Spring
  2. Spring Boot DevTools
  3. Spring AOP
  4. Spring Cloud Components

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW