Skip to content

Instantly share code, notes, and snippets.

@rafaelpontezup
Last active October 31, 2024 17:24
Show Gist options
  • Save rafaelpontezup/e4765384523b5e64296549cfd000eeda to your computer and use it in GitHub Desktop.
Save rafaelpontezup/e4765384523b5e64296549cfd000eeda to your computer and use it in GitHub Desktop.
Controller with a single-line method
@RestController
class CustomerController {
@Autowired
private CustomerService service;
@PostMapping("/api/customers")
public void create(@Valid @RequestBody CustomerRequest request) {
service.save(request);
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.Mockito.doNothing;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@WebMvcTest(CustomerController.class)
class CustomerControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private CustomerService service;
@Test
@DisplayName("Cenário de sucesso: Criação de cliente com dados válidos")
void t1() throws Exception {
CustomerRequest validRequest = new CustomerRequest("John Doe", "[email protected]");
// Simula o comportamento do serviço
doNothing().when(service).save(validRequest);
mockMvc.perform(post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(validRequest)))
.andExpect(status().isOk());
}
@Test
@DisplayName("Cenário de erro: Campos inválidos no request")
void t2() throws Exception {
CustomerRequest invalidRequest = new CustomerRequest("", "invalid-email");
mockMvc.perform(post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(invalidRequest)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.errors").exists())
.andExpect(jsonPath("$.errors.name").value("Name is required"))
.andExpect(jsonPath("$.errors.email").value("Email is invalid"));
}
@Test
@DisplayName("Cenário de erro: Request vazio")
void t3() throws Exception {
mockMvc.perform(post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.content("{}"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.errors").exists());
}
}
@ExtendWith(MockitoExtension.class)
class CustomerControllerTest {
@Mock
private CustomerService service;
@InjectMocks
private CustomerController controller;
@Test
@DisplayName("Happy-path")
void t1() {
// Arrange
CustomerRequest request = new CustomerRequest();
request.setName("John Doe");
request.setEmail("[email protected]");
// Act
controller.create(request);
// Assert
verify(service, times(1)).save(any(CustomerRequest.class));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment