Merge pull request #12661 from dkapil/task/JAVA-13636

Task/java 13636
This commit is contained in:
Loredana Crusoveanu 2022-08-31 10:17:27 +03:00 committed by GitHub
commit e7c4294e3e
2 changed files with 38 additions and 32 deletions

View File

@ -58,7 +58,6 @@
</build>
<properties>
<spring-boot.version>2.6.4</spring-boot.version>
<spring-framework.version>5.3.20</spring-framework.version>
<!-- testing -->
<mockito.version>4.6.1</mockito.version>

View File

@ -1,6 +1,7 @@
package com.baeldung.junit5.mockito;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
@ -8,7 +9,6 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -26,21 +26,28 @@ import com.baeldung.junit5.mockito.service.Errors;
import com.baeldung.junit5.mockito.service.UserService;
@ExtendWith(MockitoExtension.class)
public class UserServiceUnitTest {
class UserServiceUnitTest {
UserService userService;
SettingRepository settingRepository;
@Mock UserRepository userRepository;
@Mock MailClient mailClient;
private UserService userService;
User user;
private SettingRepository settingRepository;
@Mock
private UserRepository userRepository;
@Mock
private MailClient mailClient;
private User user;
@BeforeEach
void init(@Mock SettingRepository settingRepository) {
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
lenient().when(settingRepository.getUserMinAge()).thenReturn(10);
lenient().when(settingRepository.getUserMinAge())
.thenReturn(10);
when(settingRepository.getUserNameMinLength()).thenReturn(4);
lenient().when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false);
lenient().when(userRepository.isUsernameAlreadyExists(any(String.class)))
.thenReturn(false);
this.settingRepository = settingRepository;
}
@ -66,7 +73,7 @@ public class UserServiceUnitTest {
// Then
verify(userRepository).insert(user);
Assertions.assertNotNull(user.getId());
assertNotNull(user.getId());
verify(mailClient).sendUserRegistrationMail(insertedUser);
}
@ -80,7 +87,7 @@ public class UserServiceUnitTest {
userService.register(user);
fail("Should give an error");
} catch (Exception ex) {
assertEquals(ex.getMessage(), Errors.USER_NAME_SHORT);
assertThat(ex.getMessage()).isEqualTo(Errors.USER_NAME_SHORT);
}
// Then
@ -97,7 +104,7 @@ public class UserServiceUnitTest {
userService.register(user);
fail("Should give an error");
} catch (Exception ex) {
assertEquals(ex.getMessage(), Errors.USER_AGE_YOUNG);
assertThat(ex.getMessage()).isEqualTo(Errors.USER_AGE_YOUNG);
}
// Then
@ -116,7 +123,7 @@ public class UserServiceUnitTest {
userService.register(user);
fail("Should give an error");
} catch (Exception ex) {
assertEquals(ex.getMessage(), Errors.USER_NAME_DUPLICATE);
assertThat(ex.getMessage()).isEqualTo(Errors.USER_NAME_DUPLICATE);
}
// Then