Testing Spring Boot applications

As introduced before, Spring Boot is a project of the Spring portfolio aimed to simplify the development of Spring applications. The main benefits of using Spring Boot are summarized as follows:

  • A Spring Boot application is just a Spring ApplicationContext in which the principal convention over configuration is used. Thank to this, it is faster to get started with the Spring development.
  • The annotation @SpringBootApplication is used to identify the main class in a Spring Boot project.
  • A range of non-functional features are provided out of the box: embedded servlet containers (Tomcat, Jetty, and Undertow), security, metrics, health checks, or externalized configuration.
  • A creation of standalone running applications that just run using the command java -jar (even for web applications).
  • Spring Boot command line interface (CLI) allows to run Groovy scripts for quickly prototyping with Spring.
  • Spring Boot works in the same way as any standard Java library, that is, to use it, we simply need to add the appropriate spring-boot-*.jar in our project classpath (typically using build tools such as Maven or Gradle). Spring Boot provides a number of starters aimed to ease the process of adding the different libraries to the classpath. The following table contains several of those starters:

Name

Description

spring-boot-starter

Core starter, including auto-configuration support and logging

spring-boot-starter-batch

Starter for using Spring Batch

spring-boot-starter-cloud-connectors

Starter for using Spring Cloud Connectors, which simplifies connecting to services in Cloud platforms like Cloud Foundry and Heroku

spring-boot-starter-data-jpa

Starter for using Spring Data JPA with Hibernate

spring-boot-starter-integration

Starter for using Spring Integration

spring-boot-starter-jdbc

Starter for using JDBC with the Tomcat JDBC connection pool

spring-boot-starter-test

Starter for testing Spring Boot applications with libraries, including JUnit, Hamcrest, and Mockito

spring-boot-starter-thymeleaf

Starter for building MVC web applications using Thymeleaf views

spring-boot-starter-web

Starter for building web, including REST, applications using Spring MVC. Uses Tomcat as the default embedded container

spring-boot-starter-websocket

Starter for building WebSocket applications using Spring Framework’s WebSocket support

For complete information about Spring Boot visit the official reference: https://projects.spring.io/spring-boot/.

Spring Boot provides different capabilities to simplify the tests. For instance, it provides the @SpringBootTest annotation, which is used at classlevel in test classes. This annotation will create ApplicationContext for these tests (similarly to @ContextConfiguration but for Spring Boot based applications). As we have seen in the section before, in the spring-test module, we use the annotation @ContextConfiguration(classes=… ) to specify, which bean definition (Spring @Configuration) to be loaded. When testing Spring Boot applications this is often not required. Spring Boot’s tests annotations will search the primary configuration automatically if not explicitly define one. The search algorithm works up from the package that contains the test until it finds a @SpringBootApplication annotated class.

Spring Boot also facilitates the use of mocks for Spring components. To that, the annotation @MockBean is provided. This annotation allows defining a Mockito mock for a bean inside our ApplicationContext. It can be new beans, but also to it can replace a single existing bean definition. Mock beans are automatically reset after each test method. This method is usually known as in-container testing, in counterpart to out-of-container, in which a mock library (example, Mockito) is used to unit test the Spring components in isolation and without the need of a Spring ApplicationContext. For example of both types of unit tests for Spring applications is shown in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.209.131