liveBook · Manning

@SpringBootTest

As explained in Spring Security in Action, Second Edition

@SpringBootTest is a Spring Boot test annotation that loads an application context for integration testing.

@SpringBootTest loads and manages the Spring context for a test suite. In security tests, it provides the application environment in which endpoint behavior can be exercised. section 18.1

Use it with endpoint-focused security tests:

  • Combine it with @AutoConfigureMockMvc and an autowired MockMvc field to call endpoints without starting a real client. section 18.1
  • Exercise authentication and authorization by supplying either an empty or mock SecurityContext. section 18.1
  • Verify that an unauthenticated request receives 401 Unauthorized while a mocked authenticated request succeeds with 200 OK. section 18.1

Minimal security test setup

@SpringBootTest @AutoConfigureMockMvc public class MainTests { @Autowired private MockMvc mvc; } section 18.1

Listing 18.1 A class for writing the tests
@SpringBootTest             #1
public class MainTests {

}

View the minimal test class that uses @SpringBootTest to manage the Spring context. section 18.1

How Spring Security in Action, Second Edition covers it

How deep it goes
Defines context management and endpoint-test integration, but does not detail Spring context internals.
Examples
Uses MainTests, MockMvc calls to /hello, and 401 or 200 status assertions.
What it assumes
Assumes a Spring Boot Maven test project and familiarity with JUnit test classes.
What you can do after
Evaluate endpoint authorization by running MockMvc requests inside a managed Spring context.
How it connects
Connects Spring context management with MockMvc, SecurityContext, @WithMockUser, and RequestPostProcessor.

Also explained in

Compare how 2 books explain @SpringBootTest →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.