Middle
What tests do you write and how?
sobes.tech AI
Answer from AI
I write the following types of tests:
-
Unit Tests:
- Test individual classes or methods in isolation.
- Use JUnit or TestNG.
- Apply Mockito for creating mocks and stubs of dependencies.
- Focus on covering the logic of the method.
- Usually one test class for each tested class.
// Example of a unit test with JUnit and Mockito import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; class MyServiceTest { @Test void testProcessData() { // Mock the dependency MyDependency mockDependency = mock(MyDependency.class); when(mockDependency.getData()).thenReturn("test data"); // Create an instance of the class under test with the mock MyService myService = new MyService(mockDependency); // Call the method under test String result = myService.processData(); // Check the result assertEquals("PROCESSED: test data", result); // Verify interaction with the mock verify(mockDependency, times(1)).getData(); } } -
Integration Tests:
- Test the interaction between multiple components or services.
- Use Spring Boot Test for testing controllers, services, and database integration.
- Can use Testcontainers to run dependent services (databases, message brokers) in Docker containers.
- Less detailed than unit tests.
- Focus on the correctness of data transfer and calls between components.
// Example of an integration test with Spring Boot Test import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.ResponseEntity; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.springframework.http.HttpStatus.OK; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class MyControllerIntegrationTest { @Autowired private TestRestTemplate restTemplate; @Test void testEndpoint() { // Call the REST endpoint ResponseEntity<String> response = restTemplate.getForEntity("/my-endpoint", String.class); // Check HTTP status and response body assertEquals(OK, response.getStatusCode()); assertEquals("Success!", response.getBody()); } } -
Functional/Acceptance Tests:
- Test the application's functionality from the user's perspective.
- Can use various frameworks depending on the application type (e.g., Selenium for web apps, Rest-Assured for APIs).
- Focused on executing user scenarios.
- Test "black box".
Practices and methods:
-
TDD (Test-Driven Development):
- Write a test first that fails.
- Then write minimal code to pass the test.
- Refactor code and tests.
- Practice TDD when possible and appropriate.
-
Code coverage:
- Monitor code coverage with tools like JaCoCo.
- Aim for high coverage but not necessarily 100%, as it is not always justified.
-
Clean tests:
- Apply principles of F.I.I.R.S.T. (Fast, Isolated, Repeatable, Self-validating, Timely) in writing tests.
- Tests should be readable and maintainable.
-
Automation:
- Integrate test execution into CI/CD pipelines.
- Tests run automatically on each build or change pull.