EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Test
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot Test

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.

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.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

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

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • 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
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more