Skip to content

Commit c83b76e

Browse files
author
muriloalvesdev
committed
test for the case where the search should work and should also return exception
1 parent 4999097 commit c83b76e

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/test/java/br/com/developers/login/service/impl/UserServiceImplTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import static org.mockito.Mockito.verify;
1111
import java.util.HashSet;
1212
import java.util.Optional;
13+
import java.util.UUID;
1314
import org.junit.jupiter.api.BeforeEach;
1415
import org.junit.jupiter.params.ParameterizedTest;
1516
import org.junit.jupiter.params.provider.ArgumentsSource;
17+
import org.junit.jupiter.params.provider.ValueSource;
1618
import org.mockito.BDDMockito;
1719
import org.springframework.security.authentication.AuthenticationManager;
1820
import org.springframework.security.crypto.password.PasswordEncoder;
@@ -28,6 +30,7 @@
2830
import br.com.developers.exception.UserNotFoundException;
2931
import br.com.developers.login.dto.LoginDTO;
3032
import br.com.developers.login.dto.RegisterDTO;
33+
import br.com.developers.login.dto.UserDTO;
3134
import br.com.developers.provider.LoginDTOProviderTest;
3235
import br.com.developers.provider.RegisterDTOProviderTests;
3336

@@ -167,6 +170,35 @@ void shouldTryDeleteAndReturnUserNotFoundException(LoginDTO loginDTO) {
167170
verify(this.userRepository, times(1)).findByEmail(anyString());
168171
}
169172

173+
@ParameterizedTest
174+
@ValueSource(strings = {"926c66c4-78f2-45c8-ae52-9c661d1ebb03"})
175+
void shouldTryFindAndReturnUserNotFoundException(String id) {
176+
BDDMockito.given(this.userRepository.findById(UUID.fromString(id)))
177+
.willReturn(Optional.empty());
178+
179+
Exception exception = assertThrows(Exception.class, () -> {
180+
this.service.find(id);
181+
});
182+
183+
assertTrue(exception instanceof UserNotFoundException);
184+
assertEquals("Fail! -> Cause: User not found with id [" + id + "]", exception.getMessage());
185+
186+
verify(this.userRepository, times(1)).findById(any());
187+
}
188+
189+
@ParameterizedTest
190+
@ValueSource(strings = {"926c66c4-78f2-45c8-ae52-9c661d1ebb03"})
191+
void shouldFind(String id) {
192+
BDDMockito.given(this.userRepository.findById(UUID.fromString(id)))
193+
.willReturn(Optional.of(this.user));
194+
195+
UserDTO dto = this.service.find(id);
196+
197+
assertEquals(this.user.getFirstName(), dto.getName());
198+
assertEquals(this.user.getEmail(), dto.getEmail());
199+
verify(this.userRepository, times(1)).findById(any());
200+
}
201+
170202
@ParameterizedTest
171203
@ArgumentsSource(LoginDTOProviderTest.class)
172204
void shouldDeleteUser(LoginDTO loginDTO) {

0 commit comments

Comments
 (0)