Sobes.tech
Junior — Middle

Do you create automated tests for controllers in your projects?

sobes.tech AI

Answer from AI

Yes, it is worth creating automated tests for controllers in projects, especially if the application has complex request processing logic. In Java, frameworks like Spring Boot with MockMvc or WebTestClient are often used for this purpose. Such tests allow verifying the correctness of routing, request handling, and responses without starting a full server.

Example of a controller test using Spring Boot and MockMvc:

@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
public class MyControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testGetEndpoint() throws Exception {
        mockMvc.perform(get("/api/data"))
               .andExpect(status().isOk())
               .andExpect(content().string("expected response"));
    }
}

This approach helps quickly identify errors and regressions in controller logic.