Capítulo 5: Testes REST API
Introdução
Sua REST API deve responder corretamente às requisições HTTP. MockMvc permite testar seus endpoints sem iniciar um servidor web real.
🎯 Objetivos do Capítulo
Ao final deste capítulo, você saberá:
- ✅ Utilizar MockMvc para testar endpoints
- ✅ Testar GET, POST, PUT, DELETE
- ✅ Validar respostas JSON
- ✅ Testar erros e exceções
- ✅ Testar com autenticação
1. Configuração do MockMvc
Com @WebMvcTest
@WebMvcTest(ProductController.class)
class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService;
@Test
void shouldReturnProducts() throws Exception {
mockMvc.perform(get("/api/products"))
.andExpect(status().isOk());
}
}
Com @SpringBootTest
@SpringBootTest
@AutoConfigureMockMvc // ← Configura MockMvc
class FullIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
void testWithRealServices() throws Exception {
mockMvc.perform(get("/api/products"))
.andExpect(status().isOk());
}
}
2. Teste GET
GET Simples
@Test
void shouldReturnAllProducts() throws Exception {
when(productService.findAll()).thenReturn(List.of(
new Product(1L, "Laptop", 1000.0),
new Product(2L, "Phone", 500.0)
));
mockMvc.perform(get("/api/products"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$.length()").value(2))
.andExpect(jsonPath("$[0].name").value("Laptop"))
.andExpect(jsonPath("$[1].price").value(500.0));
}
GET com Path Variable
@Test
void shouldReturnProductById() throws Exception {
when(productService.findById(1L))
.thenReturn(Optional.of(new Product(1L, "Laptop", 1000.0)));
mockMvc.perform(get("/api/products/{id}", 1))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("Laptop"));
}
GET com Query Parameters
@Test
void shouldSearchProducts() throws Exception {
when(productService.search("laptop", 500.0, 1500.0))
.thenReturn(List.of(new Product(1L, "Laptop", 1000.0)));
mockMvc.perform(get("/api/products/search")
.param("query", "laptop")
.param("minPrice", "500")
.param("maxPrice", "1500"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(1));
}
3. Teste POST
POST com JSON Body
@Test
void shouldCreateProduct() throws Exception {
Product newProduct = new Product(null, "Tablet", 600.0);
Product savedProduct = new Product(1L, "Tablet", 600.0);
when(productService.save(any(Product.class))).thenReturn(savedProduct);
mockMvc.perform(post("/api/products")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"name": "Tablet",
"price": 600.0
}
"""))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("Tablet"));
}
Com ObjectMapper
@Autowired
private ObjectMapper objectMapper;
@Test
void shouldCreateProductWithObjectMapper() throws Exception {
Product product = new Product(null, "Tablet", 600.0);
when(productService.save(any())).thenReturn(new Product(1L, "Tablet", 600.0));
mockMvc.perform(post("/api/products")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(product)))
.andExpect(status().isCreated());
}
4. Teste PUT
@Test
void shouldUpdateProduct() throws Exception {
Product updated = new Product(1L, "Updated Laptop", 1200.0);
when(productService.update(eq(1L), any())).thenReturn(updated);
mockMvc.perform(put("/api/products/{id}", 1)
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"name": "Updated Laptop",
"price": 1200.0
}
"""))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Updated Laptop"))
.andExpect(jsonPath("$.price").value(1200.0));
}
5. Teste DELETE
@Test
void shouldDeleteProduct() throws Exception {
doNothing().when(productService).delete(1L);
mockMvc.perform(delete("/api/products/{id}", 1))
.andExpect(status().isNoContent());
verify(productService).delete(1L);
}
6. Testando Erros
404 Not Found
@Test
void shouldReturn404WhenProductNotFound() throws Exception {
when(productService.findById(999L))
.thenThrow(new ProductNotFoundException(999L));
mockMvc.perform(get("/api/products/{id}", 999))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").value("Product not found: 999"));
}
400 Bad Request (Validação)
@Test
void shouldReturn400WhenValidationFails() throws Exception {
mockMvc.perform(post("/api/products")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"name": "",
"price": -100
}
"""))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.errors").isArray())
.andExpect(jsonPath("$.errors[*].field").value(hasItem("name")));
}
500 Internal Server Error
@Test
void shouldReturn500OnServerError() throws Exception {
when(productService.findAll())
.thenThrow(new RuntimeException("Database connection failed"));
mockMvc.perform(get("/api/products"))
.andExpect(status().isInternalServerError());
}
7. Expressões JSONPath
Sintaxe Comum
// Valor simples
jsonPath("$.name").value("Laptop")
// Elemento do array
jsonPath("$[0].name").value("First")
jsonPath("$[1].price").value(100.0)
// Tamanho do array
jsonPath("$.length()").value(5)
jsonPath("$").isArray()
// Existência
jsonPath("$.id").exists()
jsonPath("$.deleted").doesNotExist()
// Tipo
jsonPath("$.price").isNumber()
jsonPath("$.name").isString()
jsonPath("$.items").isArray()
// Comparação
jsonPath("$.price").value(greaterThan(100.0)))
jsonPath("$.items.length()").value(lessThanOrEqualTo(10)))
// Wildcard
jsonPath("$[*].name").value(hasItem("Laptop"))
jsonPath("$.items[*].price").value(everyItem(greaterThan(0.0))))
8. Teste com Autenticação
Sem Security (Mock)
@WebMvcTest(ProductController.class)
@AutoConfigureMockMvc(addFilters = false) // ← Desativa filtros de Security
class ProductControllerNoSecurityTest {
// Testes sem autenticação
}
Com @WithMockUser
@WebMvcTest(ProductController.class)
class SecuredControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
@WithMockUser(username = "admin", roles = {"ADMIN"})
void adminCanAccessAdminEndpoint() throws Exception {
mockMvc.perform(get("/api/admin/products"))
.andExpect(status().isOk());
}
@Test
@WithMockUser(username = "user", roles = {"USER"})
void userCannotAccessAdminEndpoint() throws Exception {
mockMvc.perform(get("/api/admin/products"))
.andExpect(status().isForbidden());
}
@Test
void anonymousCannotAccess() throws Exception {
mockMvc.perform(get("/api/products"))
.andExpect(status().isUnauthorized());
}
}
Com JWT
@Test
void shouldAccessWithValidToken() throws Exception {
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...";
mockMvc.perform(get("/api/products")
.header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
9. Debug e Logging
andDo(print())
@Test
void debugTest() throws Exception {
mockMvc.perform(get("/api/products"))
.andDo(print()) // ← Exibe a requisição e resposta completas
.andExpect(status().isOk());
}
Exemplo de Saída
MockHttpServletRequest:
HTTP Method = GET
Request URI = /api/products
Parameters = {}
Headers = []
MockHttpServletResponse:
Status = 200
Content type = application/json
Body = [{"id":1,"name":"Laptop","price":1000.0}]
10. Exemplo Completo CRUD
@WebMvcTest(ProductController.class)
class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private ProductService productService;
// CREATE
@Test
void shouldCreateProduct() throws Exception {
when(productService.save(any())).thenReturn(new Product(1L, "Laptop", 1000.0));
mockMvc.perform(post("/api/products")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"Laptop\",\"price\":1000.0}"))
.andExpect(status().isCreated())
.andExpect(header().exists("Location"))
.andExpect(jsonPath("$.id").value(1));
}
// READ ALL
@Test
void shouldReturnAllProducts() throws Exception {
when(productService.findAll()).thenReturn(List.of(
new Product(1L, "Laptop", 1000.0),
new Product(2L, "Phone", 500.0)
));
mockMvc.perform(get("/api/products"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(2));
}
// READ ONE
@Test
void shouldReturnProductById() throws Exception {
when(productService.findById(1L))
.thenReturn(Optional.of(new Product(1L, "Laptop", 1000.0)));
mockMvc.perform(get("/api/products/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Laptop"));
}
// UPDATE
@Test
void shouldUpdateProduct() throws Exception {
when(productService.update(eq(1L), any()))
.thenReturn(new Product(1L, "Updated", 1500.0));
mockMvc.perform(put("/api/products/1")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"Updated\",\"price\":1500.0}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Updated"));
}
// DELETE
@Test
void shouldDeleteProduct() throws Exception {
mockMvc.perform(delete("/api/products/1"))
.andExpect(status().isNoContent());
verify(productService).delete(1L);
}
// ERROR - Not Found
@Test
void shouldReturn404() throws Exception {
when(productService.findById(999L)).thenReturn(Optional.empty());
mockMvc.perform(get("/api/products/999"))
.andExpect(status().isNotFound());
}
// ERROR - Validation
@Test
void shouldReturn400OnInvalidData() throws Exception {
mockMvc.perform(post("/api/products")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"\",\"price\":-1}"))
.andExpect(status().isBadRequest());
}
}
11. Resumo
Métodos MockMvc
| Método | Uso |
|---|---|
get() | Requisição GET |
post() | Requisição POST |
put() | Requisição PUT |
delete() | Requisição DELETE |
patch() | Requisição PATCH |
Asserções Comuns
.andExpect(status().isOk()) // 200
.andExpect(status().isCreated()) // 201
.andExpect(status().isNoContent()) // 204
.andExpect(status().isBadRequest()) // 400
.andExpect(status().isUnauthorized()) // 401
.andExpect(status().isForbidden()) // 403
.andExpect(status().isNotFound()) // 404
.andExpect(status().isInternalServerError()) // 500
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.field").value(expected))
.andExpect(header().exists("Location"))
🎯 Próximo Passo
Hora de praticar! Os TPs vão te ajudar a dominar os testes.
➡️ TP9.1: Testes Unitários - Service Layer