Testing Spring Boot Application

Test cases are important for your application, as they not only verify the code but also make sure it does everything you expect it to do. In this chapter, we will explore how to write tests to make sure that things don't break as your application continues to evolve. You could either write tests before or after the code has been written.

Spring doesn't provide an API to write unit tests for an application. Spring promotes loose coupling and interface-driven design. So, it makes it easy to write unit tests for any Spring application. On the other hand, integration tests require some help from Spring Framework, because Spring does bean wiring between the application components in your production application. So, Spring is responsible for configuring and creating the application components in your production application.

Spring provides us with a separate module for testing—Spring test. The Spring test module provides the SpringJUnit4ClassRunner class to help load a Spring application context in JUnit-based application tests. But Spring Boot, by default, enables auto-configuring support and provides another class, SpringRunner. Spring Boot also offers a handful of useful testing utilities.

We'll start by looking at how to test with a fully Spring Boot-enabled application context. You will learn to unit test Spring Boot services and also learn to mock Spring Boot services. You'll also learn about the different tools available to test service contracts with basic usage. In this chapter, we will create a REST application by exposing some REST URIs and then we will test these using the Postman and SoapUI tools. By the end of this chapter, you will have a better understanding of how Spring Boot supports testing.

This chapter will cover the following points:

  • Test-driven development
  • JUnit test for Spring Boot
  • Using Mockito for mocking services
  • Postman for testing RESTful service contracts
  • SoapUI for testing RESTful service contracts

Let's look at these topics in detail.

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

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