add test to save user service with expection
This commit is contained in:
parent
20b84b73a2
commit
7ef69d9222
1 changed files with 24 additions and 0 deletions
|
|
@ -11,6 +11,7 @@ import org.mockito.InjectMocks;
|
|||
import org.mockito.Mock;
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import br.com.rayankonecny.userserviceapi.entity.User;
|
||||
import br.com.rayankonecny.userserviceapi.mapper.UserMapper;
|
||||
|
|
@ -105,4 +106,27 @@ public class UserServiceTest {
|
|||
verify(repository).findByEmail(request.email());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCallSaveWithInvalidEmailThenThrowDataIntegrityViolationException() {
|
||||
|
||||
final var request = generateMock(CreateUserRequest.class);
|
||||
final var entity = generateMock(User.class);
|
||||
|
||||
when(repository.findByEmail(anyString())).thenReturn(Optional.of(entity));
|
||||
|
||||
try {
|
||||
service.save(request);
|
||||
} catch (Exception ex) {
|
||||
assertEquals(DataIntegrityViolationException.class, ex.getClass());
|
||||
assertEquals("Email [" + request.email() + "] already exists", ex.getMessage());
|
||||
}
|
||||
|
||||
verify(repository).findByEmail(request.email());
|
||||
verify(mapper, times(0)).fromRequest(any());
|
||||
verify(encoder, times(0)).encode(anyString());
|
||||
verify(repository, times(0)).save(any(User.class));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue