Dev8

Module 8: Testing and Debugging (Java)

This module covers unit testing, integration testing, and debugging techniques to ensure the reliability of backend systems.

8.1 Learning Objectives

  • Write unit tests using JUnit and Mockito.
  • Perform integration testing for RESTful APIs.
  • Debug backend applications effectively.

8.2 JUnit Example


import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {
    @Test
    void testAddition() {
        assertEquals(4, 2 + 2);
    }
}
  

8.3 Integration Test Example


@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void shouldReturnHello() throws Exception {
        mockMvc.perform(get("/user"))
               .andExpect(status().isOk())
               .andExpect(content().string("John Doe"));
    }
}
  

8.4 Exercise

  • Write unit and integration tests for your existing API.
  • Use breakpoints and logs to debug application flow.

Latest Notes

View Archive [ -> ]