add test to find all
This commit is contained in:
parent
712d28f869
commit
6fbc769195
1 changed files with 34 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ package br.com.rayankonecny.userserviceapi.service;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
|
@ -14,6 +15,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|||
import br.com.rayankonecny.userserviceapi.entity.User;
|
||||
import br.com.rayankonecny.userserviceapi.mapper.UserMapper;
|
||||
import br.com.rayankonecny.userserviceapi.repository.UserRepository;
|
||||
import models.exceptions.ResourceNotFoundException;
|
||||
import models.responses.UserResponse;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
|
@ -51,4 +53,36 @@ public class UserServiceTest {
|
|||
verify(repository, times(1)).findById(anyString());
|
||||
verify(mapper, times(1)).fromEntity(any(User.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallFindByIdWithInvalidIdThenThrowResourceNotFoundException() {
|
||||
when(repository.findById(anyString())).thenReturn(Optional.empty());
|
||||
|
||||
try {
|
||||
service.findById("1");
|
||||
} catch (Exception ex) {
|
||||
assertEquals(ResourceNotFoundException.class, ex.getClass());
|
||||
assertEquals("Object not found! Id: 1, Type: UserResponse", ex.getMessage());
|
||||
}
|
||||
|
||||
verify(repository, times(1)).findById(anyString());
|
||||
verify(mapper, times(0)).fromEntity(any(User.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallFindAllThenReturnListOfUserResponses() {
|
||||
// Implement test for findAll method
|
||||
when(repository.findAll()).thenReturn(List.of(new User(), new User()));
|
||||
when(mapper.fromEntity(any(User.class))).thenReturn(mock(UserResponse.class));
|
||||
|
||||
final var responses = service.findAll();
|
||||
|
||||
assertNotNull(responses);
|
||||
assertEquals(2, responses.size());
|
||||
assertEquals(UserResponse.class, responses.get(0).getClass());
|
||||
|
||||
verify(repository, times(1)).findAll();
|
||||
verify(mapper, times(2)).fromEntity(any(User.class));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue