Skip to content

Pull Request #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
# springboot-demo
# Customer Management Service - Testing

## Overview

This project focuses on the implementation of robust and comprehensive testing for a REST API-based Customer Management Service. The testing framework incorporates tools like JUnit, Mockito, Testcontainers, and Jacoco to ensure reliability and maintainability.

## Testing Tools and Frameworks
- **JUnit 5:** For writing and running unit and integration tests.
- **Mockito:** Used to mock dependencies and isolate the units being tested.
- **Testcontainers:** Enables integration testing with a PostgreSQL database in a containerized environment.
- **Jacoco:** Provides code coverage reporting to ensure the adequacy of test coverage.

## Key Features of the Testing Suite

1. **Unit Tests:**

- Ensure the correctness of service and controller logic.
Dependencies are mocked using Mockito to isolate the unit under test.

**Integration Tests:**

- Verify end-to-end functionality of the REST API with a real database (PostgreSQL) using Testcontainers.
Test scenarios include CRUD operations and edge cases.

3. **Code Coverage:**

- Jacoco generates detailed coverage reports, ensuring all critical code paths are tested.

## Running the Tests

### Prerequisites:

- Install Java 17+ and Maven 3.6+.
- Ensure Docker is installed and running for Testcontainers.

#### Run All Tests:

```
mvn test
```
Or you can manually test all the tests for the particular test file in your IDE.

#### View Code Coverage:

After running tests, open the generated Jacoco report in the following path:

```target/site/jacoco/index.html```

Navigate to this file in your browser to view detailed coverage metrics.

#### Example Test Scenarios

1. **Unit Test**
- **CustomerServiceTest:**
Tests service-layer methods for creating, updating, and deleting customers.
Verifies exceptions like CustomerNotFoundException and CustomerEmailUnavailableException are properly thrown.

2. **Integration Test**
- **CustomerIntegrationTest**:
Uses Testcontainers to spin up a PostgreSQL container for testing the application in a realistic environment.
Covers scenarios like creating a customer, retrieving by ID, updating, and deleting.

## Contribution
Feel free to contribute to this project by adding more tests or enhancing existing ones. Open a pull request for any updates.

4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ services:
container_name: postgresql
image: postgres:16.2
environment:
POSTGRES_USER: ntloc
POSTGRES_PASSWORD: password
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- postgresql:/var/lib/postgresql/data
ports:
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<description>Demo Hello World application for Spring Boot</description>
<properties>
<java.version>17</java.version>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
</properties>
<dependencies>
<dependency>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/ntloc/demo/customer/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@


import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Table
@Entity
@Getter
@Setter
public class Customer {

@Id
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ spring:
name: customer
datasource:
url: jdbc:postgresql://localhost:5432/customer
username: ntloc
password: password
username: postgres
password: postgres
jpa:
hibernate:
ddl-auto: create-drop
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/ntloc/demo/AbstractTestContainersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ntloc.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.junit.jupiter.api.Assertions.assertTrue;

@Testcontainers
public abstract class AbstractTestContainersTest {

@Container
@ServiceConnection
static PostgreSQLContainer<?> postgreSQLContainer =
new PostgreSQLContainer<>("postgres:16.2");

@Test
void canEstablishDatabaseContainerConnection() {
assertTrue(postgreSQLContainer.isCreated(), "Database container should be created");
assertTrue(postgreSQLContainer.isRunning(), "Database container should be running");
}

}
25 changes: 0 additions & 25 deletions src/test/java/com/ntloc/demo/AbstractTestcontainersTest.java

This file was deleted.

6 changes: 5 additions & 1 deletion src/test/java/com/ntloc/demo/DemoApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.ntloc.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

//@SpringBootTest
class DemoApplicationTests {

// @Test
// @Test
void contextLoads() {
// System.out.println("Tests starter");
}

}
Loading