chapter two

2 Testing a Spring Boot "Hello World" application

 

This chapter covers

  • Interacting with your application inside a @SpringBootTest
  • Factoring common setup with JUnit
  • Decoupling from external dependencies with mocks
  • Deciding when (not) to use @SpringBootTest

When you create a project on https://start.spring.io, also known as Spring Initializr, you get testing libraries bundled in, as we mentioned in the previous chapter. There is also a test class in the zip file containing your project, ready to run. It contains a single (empty) test method, and the only thing it verifies is that the application can start. This has value on its own, as it validates your Spring dependency graph and your Spring Boot properties. Having this test class also makes it easy to add your own tests without having to make too many decisions: the next obvious step is to add another method to this test file.

2.1 Our test subject: sample application

2.2 Running the Spring-provided test

2.3 Adding your first test

2.4 Accessing context dependencies

2.5 Factoring common setup with JUnit

2.6 Decoupling from external dependencies with mocks

2.7 A word of caution: when NOT to use @SpringBootTest

2.8 Summary