From 91e0d5ed5996ce5de0286605ac539c5c984dc960 Mon Sep 17 00:00:00 2001 From: dudxor4587 <68459414+dudxor4587@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:35:06 +0900 Subject: [PATCH] [feature] add junit test example (#21) Co-authored-by: dudxor4587 --- src/main/resources/application.properties | 6 +- .../ICustomerServiceTest.java | 133 ++++++++++++++++++ 2 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/test/java/evrentan/examples/springbootprojectexample/ICustomerServiceTest.java diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index dd205e8..e4c9e2b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,4 @@ #Database connection strings -#spring.data.mongodb.host=localhost -#spring.data.mongodb.port=27017 -#spring.data.mongodb.database=spring-boot-project-example +spring.data.mongodb.host=localhost +spring.data.mongodb.port=27017 +spring.data.mongodb.database=spring-boot-project-example diff --git a/src/test/java/evrentan/examples/springbootprojectexample/ICustomerServiceTest.java b/src/test/java/evrentan/examples/springbootprojectexample/ICustomerServiceTest.java new file mode 100644 index 0000000..9c7f822 --- /dev/null +++ b/src/test/java/evrentan/examples/springbootprojectexample/ICustomerServiceTest.java @@ -0,0 +1,133 @@ +package evrentan.examples.springbootprojectexample; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import evrentan.examples.springbootprojectexample.dto.Customer; +import evrentan.examples.springbootprojectexample.dto.CustomerRef; +import evrentan.examples.springbootprojectexample.entity.CustomerEntity; +import evrentan.examples.springbootprojectexample.impl.CustomerServiceImpl; +import evrentan.examples.springbootprojectexample.mapper.CustomerMapper; +import evrentan.examples.springbootprojectexample.repository.CustomerRepository; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +class ICustomerServiceTest { + @Mock + private CustomerRepository customerRepository; + + @Mock + private CustomerMapper customerMapper; + + @InjectMocks + private CustomerServiceImpl customerService; + + @BeforeEach + void beforeEach() { + MockitoAnnotations.openMocks(this); + } + + @Test + void createCustomer() { + //given + Customer customer = new Customer(); + customer.setId("1"); + customer.setCustomerFullName("John"); + customer.setCustomerType("ACTIVE"); + + CustomerEntity customerEntity = new CustomerEntity(); + customerEntity.setId("1"); + customerEntity.setCustomerType("ACTIVE"); + customerEntity.setCustomerFullName("John"); + + //when + when(customerMapper.toEntity(customer)).thenReturn(customerEntity); + when(customerRepository.save(any())).thenReturn(customerEntity); + when(customerMapper.toDto(customerEntity)).thenReturn(customer); + + Customer customer2 = customerService.createCustomer(customer); + + //then + verify(customerRepository, times(1)).save(any()); + assertEquals(customer, customer2); + } + + @Test + void getAllCustomerRefs() { + //given + Customer customer = new Customer(); + customer.setId("1"); + customer.setCustomerFullName("John"); + customer.setCustomerType("ACTIVE"); + + CustomerEntity customerEntity = new CustomerEntity(); + customerEntity.setId("1"); + customerEntity.setCustomerType("ACTIVE"); + customerEntity.setCustomerFullName("John"); + + //when + when(customerMapper.toEntity(customer)).thenReturn(customerEntity); + when(customerRepository.save(any())).thenReturn(customerEntity); + when(customerMapper.toDto(customerEntity)).thenReturn(customer); + + //given + CustomerRef customerRef = new CustomerRef(); + customerRef.setId("1"); + + List customerRefList = new ArrayList<>(); + customerRefList.add(customerRef); + + List entityList = new ArrayList<>(); + entityList.add(customerEntity); + + when(customerMapper.toDtoRefList(any())).thenReturn(customerRefList); + when(customerRepository.findAll()).thenReturn(entityList); + + List customerRefs = customerService.getAllCustomerRefs(); + + verify(customerMapper, times(1)).toDtoRefList(any()); + verify(customerRepository, times(1)).findAll(); + + assertEquals(customerRefs, customerRefList); + } + + @Test + void getCustomerById() { + //given + Customer customer = new Customer(); + customer.setId("1"); + customer.setCustomerFullName("John"); + customer.setCustomerType("ACTIVE"); + + CustomerEntity customerEntity = new CustomerEntity(); + customerEntity.setId("1"); + customerEntity.setCustomerType("ACTIVE"); + customerEntity.setCustomerFullName("John"); + + //when + when(customerMapper.toEntity(customer)).thenReturn(customerEntity); + when(customerRepository.save(any())).thenReturn(customerEntity); + when(customerMapper.toDto(customerEntity)).thenReturn(customer); + + Customer customer2 = customerService.createCustomer(customer); + + when(customerMapper.toDto(any())).thenReturn(customer); + when(customerRepository.findById(anyString())).thenReturn(Optional.of(customerEntity)); + + Customer customerExample = customerService.getCustomerById("1"); + + verify(customerRepository, times(1)).findById(anyString()); + + assertEquals(customerExample, customer2); + } +}