Spring Boot Rest Testing

Can use @SpringBootTest for MVC applications, this annotation provides support for different webEnvironment modes and setup: RANDOM_PORT, DEFINED_PORT, MOCK, NODE.

This also configures TestRestTemplate.


Alternative to RestTemplate for integration tests.

  • takes a relative path, instead of an absolute one.
  • fault tolerant, does not throw exception when error is received
  • ignores cookies and redirects
  • can be customized with RestTemplateBuilder
public class AccountClientBootTest{

 private TestRestTemplate restTemplate;

 //Test code

MVC Test framework

Provide support for Spring MVC code, having requests processed through DispatcherServlet without container.

It uses MockMvcRequestBuilders and MockMvcResultMatchers

Slice testing

Performs isolated testing within a slice of an application, where dependencies need to be mocked.

Uses @WebMvcTest annotation and @MockBean.

public class AccountControllerBootTests{

 private MockMvc mockMvc;

 private AccountManager accountManager;

 public void someTest() throws Exception {
 // code



  • @Mock – from Mockito framework, used when Spring context is not required.
  • @MoockBean – from Spring boot, used with Spring context
  • @DataJpaTest – configures TestEntityManager an can use @AutoConfigureTestDatabase