Friday, May 19, 2023

What is @SpringBootTest Annotation in Java and Spring? How to use it? Example Tutorial

Hello guys, if you want to learn what is @SpringBootTest annotation and how to use this annotation to test your Spring Boot application then you have come to the right place. The @SpringBootTest annotation is a powerful annotation in Spring framework which is used to load entire application context for Spring Boot application and create integration test. It allows you to test your application in the same way as it runs on production. You can use this annotation to test your controller, service, repository classes and every other part of your Java Spring Boot application. Earlier, I have shared the best Spring Boot courses and Spring Boot Testing interview questions and in this article, I am going to explain to you about @SpringBootTest annotation. Before diving into how to use @SpringBootTest annotation. It is good to briefly introduce Testing in Spring boot. 

There is unit testing and integration test. In a nutshell, when you do a unit test, you only test just a single unit of code, one method at a time, excluding all other components. Other components you exclude would be tested separately and that is another unit test. 

On the other hand, the Integration test is bringing all the unit tests together making them work without breaking any units of codes. A particular unit of code can of course work on its own. 

So, if you bring many of the units tests together and it works altogether Congratulations! It means your integration test is fine. But if not, it means the integration test fails. And you have to fix that because each unit of the test must first work on its own and then work altogether with the rest of the units of the test without breaking anything.

Spring Boot Annotations is a form of metadata in spring boot that provides data about a program that is not a part of the program itself. In other words, annotations are used to provide supplemental information about a program.

What is Spring boot? It’s a web framework with 3 important segments which are M, V, C-- Model, Views, and Controller. So when you create your project using spring boot initializer, and you download the zipped file of the project. When you extract it, what it looks like is the following image you see below. you have a readMe file with MD extension.

What is @SpringBootTest Annotation in Java? When and How to use it? Example Tutorial

 
And your pom file as well is where you put all dependencies needed for your project. You would always see src/main/java also with the corresponding src/tests/java. But, the test file is empty. So You are the one to start writing your tests. 

By default, whatever you called your application is what you would have as a test. For instance, in the image below You have DemoApplication as the name of the application and the test folder is DemoApplicationTest.

What is @SpringBootTest annotation in Spring Boot? How to use it? Example Tutorial

Fig 1.0: Spring boot project.

This is how your directories and files are when you start the spring boot project, Your test folder is where all manner of tests takes place. You can always have more than one test to separate your concerns.

Spring Boot Annotations do not use XML(Extensible markup language) and instead use the convention over configuration principle. Every new Spring Boot project comes with an initial test inside src/test/resources that uses this annotation. 

Though Spring Boot can use regular Spring tests using the @Test annotation, it has a special annotation—@SpringBootTest—that lets you test using Spring-boot specific features. There are other options, such as @WebMvcTest and @DataJpaTest to use, But @SpringBootTest works best when testing the whole application together.

With the @SpringBootTest annotation, Spring Boot provides a convenient way to start up an application context to be used in a test. In this tutorial, we’ll discuss when to how to use @SpringBootTest.



When to use @SpringBootTest Annotation in Java? Example

Before you can use @SpringbootTest, There is a maven dependency you have to install in your pom.xml file which is called spring-boot-starter-test.

Spring- boot- starter- test is the primary dependency that contains the majority of elements required for our tests. It is the common test dependency to use which imports both Spring Boot test modules as well as
  • junit4: The standard for unit testing Java applications.
  • Spring Test & Spring Boot Test: Utilities and integration test support for Spring Boot applications.
  • AssertJ: A fluent assertion library.
  • hamcrest: A library of matcher objects (also known as constraints or predicates).
  • Mockito: A Java mocking framework.
  • JsonAssert: An assertion library for JSON.
  • JsonPath: XPath for JSON.

All these listed above are the scope of the spring boot starter dependency, and they are all very useful in writing spring boot tests. But, If they do not suit you, then you would have to add test dependencies of your own.

If you do not have the spring- boot- starter- test dependency installed in your pom file you would not be able to use @SpringbootTest. Below is how your pom file looks like, where you add dependencies needed.


  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-test</artifactId>
  5. <scope>test</scope>
  6. <version>2.5.0</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.h2database</groupId>
  10. <artifactId>h2</artifactId>
  11. <scope>test</scope>
  12. </dependency>
  13. </dependencies>

When you open your test folder, there is a test inside, and at the top of the class, it is annotated ”@SpringBootTest” which brings your application to the test context

Below is how it looks like.

  1. @SpringBootTest
  2. class DemoApplicationTests {
  3.  
  4. @Test
  5. void contextLoads() {
  6. //write your test here
  7. }
  8. }


That' all  about what is @SpringBootTest annotation and when to use it. As I said, you can use this annotation to load entire application context and also perform both unit test and integration test op your spring boot application. You can use this to test your Controller, Service, and Repository classes. 

The @SpringBootTest annotation also provides several configuration options, including loading specific classes, setting the environment, and setting properties, making it a versatile tool that can be configured to suit a wide range of testing scenarios. 

You can also use @WebMvcTest and @DataJpaTest to test the service layer and data layer but when it comes to testing the whole application you can use @SpringBootTest annotation.  The flexibility and power of the @SpringBootTest annotation make it an indispensable tool for Java developers working with Spring Boot.

Other Java and Spring Tutorial you may like
  • How to update an entity using Spring Data JPA? (example)
  • What is @Conditional annotation in Spring? (conditional example)
  • How Spring MVC works internally? (answer)
  • Spring Data JPA @Query Example (query example)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • Difference between @RequestParam and @PathVariable in Spring (answer)
  • Top 7  Courses to learn Microservices in Java (courses)
  • How to use @Bean in Spring framework (Example)
  • How to fix No property type found in Spring Data JPA? [Solution]
  • Spring Data JPA Repository (JpaReposistory example)
  • 20+ Spring MVC Interview Questions for Programmers (answer)
  • 13 Spring Boot Actuator Interview questions (boot questions)
  • Difference between @Autowired and @Inject in Spring? (answer)
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • 5 Spring Cloud annotations Java programmer should learn (cloud)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • 5 Courses to Learn Spring Security for Java programmers (courses)
  • 10 Spring MVC annotations Java developer should know (annotations)
  • @SpringBootApplication vs. @EnableAutoConfiguration? (answer)
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • Difference between @Component, @Service, and @Controller in Spring (answer)

Thanks for reading this article so far. If you find this Spring Boot Testing tutorial and Example, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If you are a beginner and want to learn the Spring Boot framework from scratch and look for some of the best online resources, you can also check out these best Spring Boot courses for Java developersThis list contains free Udemy and Pluralsight courses to learn Spring Boot from scratch.      

No comments :

Post a Comment