This project contains hands-on labs for learning and practicing unit testing in Java using Mockito and TestNG. Each lab focuses on a specific concept, with simple, clear code examples and corresponding tests.
- Lab 1: Mocking a Simple Service and Verifying Mock Interactions
- Lab 2: Writing a Simple TestNG Test and Using Data Providers
- Project Structure
- Setup and Running Tests
Location:
- Main code:
testlabs/src/main/java/com/elitekaycy/testlabs/labs/simpleService/ - Test:
testlabs/src/test/java/com/elitekaycy/testlabs/labs/MockTest.java
This lab demonstrates how to use Mockito to mock dependencies and verify interactions in a service class. The example is a simple user service that fetches user data from a repository.
- User.java: A simple POJO representing a user with
nameandemail. - UserRepository.java: Simulates a data repository with a method to find a user by name.
- UserService.java: Service class that uses
UserRepositoryto fetch user data.
- MockTest.java:
- Uses Mockito to mock
UserRepository. - Injects the mock into
UserService. - Tests that
UserService.getUser(name)returns the expected user. - Verifies that the repository's
findUserByNamemethod was called.
- Uses Mockito to mock
@Mock
private UserRepository userRepository;
@InjectMocks
private UserService userService;
@Test
public void testGetUser() {
String name = "dickson";
User user = new User(name, "dickson@gmail.com");
when(userRepository.findUserByName(name)).thenReturn(user);
User userResult = userService.getUser(name);
assertNotNull(userResult);
assertEquals(userResult.toString(), user.toString());
verify(userRepository).findUserByName(name);
}Location:
- Main code:
testlabs/src/main/java/com/elitekaycy/testlabs/labs/simpleSort/QuickSort.java - Test:
testlabs/src/test/java/com/elitekaycy/testlabs/labs/QuickSortTest.java
This lab demonstrates how to write unit tests for a sorting algorithm using TestNG, including the use of data providers for parameterized testing.
- QuickSort.java:
- Contains a static
sort(int[] arr)method that sorts an array in ascending order using a simple (non-recursive) sorting algorithm.
- Contains a static
- QuickSortTest.java:
- Tests the
QuickSort.sortmethod with hardcoded arrays. - Uses a TestNG
@DataProviderto supply multiple test cases. - Asserts that the output array is sorted as expected.
- Tests the
@Test
public void unit_test_quicksort() {
int[] arr = { 4, 3, 7, 1 };
int[] result = QuickSort.sort(arr);
int[] expected = { 1, 3, 4, 7 };
for (int i = 0; i < result.length; i++) {
assertEquals(expected[i], result[i]);
}
}
@Test(dataProvider = "sortData")
public void test_quicksort_with_data_providers(int[] arr) {
int[] result = QuickSort.sort(arr);
assertTrue(result[0] <= result[1]);
assertTrue(result[0] <= result[result.length - 1]);
}
@DataProvider(name = "sortData")
public Object[][] sortData() {
return new Object[][] { { new int[] { 4, 3, 7, 1 } }, { new int[] { 17, 19, 12 } } };
}testlabs/
├── pom.xml
├── readme.md
├── src/
│ ├── main/
│ │ └── java/
│ │ └── com/elitekaycy/testlabs/
│ │ └── labs/
│ │ ├── simpleService/
│ │ │ ├── User.java
│ │ │ ├── UserRepository.java
│ │ │ └── UserService.java
│ │ └── simpleSort/
│ │ └── QuickSort.java
│ └── test/
│ └── java/
│ └── com/elitekaycy/testlabs/
│ ├── labs/
│ │ ├── MockTest.java
│ │ └── QuickSortTest.java
│ └── DemoTest.java
- Java 21+
- Maven
The project uses the following dependencies (see testlabs/pom.xml):
- TestNG for unit testing
- Mockito for mocking
To run all tests, use:
cd testlabs
mvn test- The
DemoTest.javafile is a simple demonstration and not part of the main labs. - The labs are designed to be minimal and focused on demonstrating core testing concepts.
This project includes a set of Spring Boot microservices demonstrating OAuth2 security, API Gateway, and service discovery using Eureka. Each service is independently runnable and configured for integration in a microservices architecture.
Oauth/
├── Albums-Resource-Server/
├── ApiGateway/
├── DiscoveryService/
├── Photos-Resource-Server/
├── Docker-Compose.yaml
- Path:
Oauth/DiscoveryService - Purpose: Eureka server for service discovery.
- Key Tech: Spring Cloud Netflix Eureka.
- Entry Point:
DiscoveryServiceApplication.java - How to run:
mvn spring-boot:runin theDiscoveryServicedirectory.
- Path:
Oauth/ApiGateway - Purpose: API Gateway for routing and securing requests.
- Key Tech: Spring Cloud Gateway, Eureka client.
- Entry Point:
ApiGatewayApplication.java - How to run:
mvn spring-boot:runin theApiGatewaydirectory.
- Path:
Oauth/Albums-Resource-Server - Purpose: OAuth2-protected resource server exposing album data.
- Key Tech: Spring Boot, OAuth2 Resource Server.
- Entry Point:
AlbumsApplication.java - How to run:
mvn spring-boot:runin theAlbums-Resource-Serverdirectory.
- Path:
Oauth/Photos-Resource-Server - Purpose: OAuth2-protected resource server exposing photo data.
- Key Tech: Spring Boot, OAuth2 Resource Server.
- Entry Point:
PhotosApplication.java - How to run:
mvn spring-boot:runin thePhotos-Resource-Serverdirectory.
- File:
Oauth/Docker-Compose.yaml - Purpose: Orchestrates all services for local development.
Note:
- All services use Java 11 and Spring Boot 2.3.x.
- The resource servers require OAuth2 tokens for access.
- The API Gateway and resource servers register themselves with the Eureka Discovery Service.