commit
e7c4294e3e
|
@ -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>
|
||||
|
|
|
@ -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,31 +26,38 @@ import com.baeldung.junit5.mockito.service.Errors;
|
|||
import com.baeldung.junit5.mockito.service.UserService;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class UserServiceUnitTest {
|
||||
|
||||
UserService userService;
|
||||
SettingRepository settingRepository;
|
||||
@Mock UserRepository userRepository;
|
||||
@Mock MailClient mailClient;
|
||||
class UserServiceUnitTest {
|
||||
|
||||
private UserService userService;
|
||||
|
||||
private SettingRepository settingRepository;
|
||||
|
||||
@Mock
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Mock
|
||||
private MailClient mailClient;
|
||||
|
||||
private User user;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) {
|
||||
// Given
|
||||
user = new User("Jerry", 12);
|
||||
when(userRepository.insert(any(User.class))).then(new Answer<User>() {
|
||||
int sequence = 1;
|
||||
|
||||
|
||||
@Override
|
||||
public User answer(InvocationOnMock invocation) throws Throwable {
|
||||
User user = (User) invocation.getArgument(0);
|
||||
|
@ -63,10 +70,10 @@ public class UserServiceUnitTest {
|
|||
|
||||
// When
|
||||
User insertedUser = userService.register(user);
|
||||
|
||||
|
||||
// Then
|
||||
verify(userRepository).insert(user);
|
||||
Assertions.assertNotNull(user.getId());
|
||||
assertNotNull(user.getId());
|
||||
verify(mailClient).sendUserRegistrationMail(insertedUser);
|
||||
}
|
||||
|
||||
|
@ -74,53 +81,53 @@ public class UserServiceUnitTest {
|
|||
void givenShortName_whenSaveUser_thenGiveShortUsernameError() {
|
||||
// Given
|
||||
user = new User("tom", 12);
|
||||
|
||||
|
||||
// When
|
||||
try {
|
||||
userService.register(user);
|
||||
fail("Should give an error");
|
||||
} catch(Exception ex) {
|
||||
assertEquals(ex.getMessage(), Errors.USER_NAME_SHORT);
|
||||
} catch (Exception ex) {
|
||||
assertThat(ex.getMessage()).isEqualTo(Errors.USER_NAME_SHORT);
|
||||
}
|
||||
|
||||
|
||||
// Then
|
||||
verify(userRepository, never()).insert(user);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void givenSmallAge_whenSaveUser_thenGiveYoungUserError() {
|
||||
// Given
|
||||
user = new User("jerry", 3);
|
||||
|
||||
|
||||
// When
|
||||
try {
|
||||
userService.register(user);
|
||||
fail("Should give an error");
|
||||
} catch(Exception ex) {
|
||||
assertEquals(ex.getMessage(), Errors.USER_AGE_YOUNG);
|
||||
} catch (Exception ex) {
|
||||
assertThat(ex.getMessage()).isEqualTo(Errors.USER_AGE_YOUNG);
|
||||
}
|
||||
|
||||
|
||||
// Then
|
||||
verify(userRepository, never()).insert(user);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void givenUserWithExistingName_whenSaveUser_thenGiveUsernameAlreadyExistsError() {
|
||||
// Given
|
||||
user = new User("jerry", 12);
|
||||
Mockito.reset(userRepository);
|
||||
when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(true);
|
||||
|
||||
|
||||
// When
|
||||
try {
|
||||
userService.register(user);
|
||||
fail("Should give an error");
|
||||
} catch(Exception ex) {
|
||||
assertEquals(ex.getMessage(), Errors.USER_NAME_DUPLICATE);
|
||||
} catch (Exception ex) {
|
||||
assertThat(ex.getMessage()).isEqualTo(Errors.USER_NAME_DUPLICATE);
|
||||
}
|
||||
|
||||
|
||||
// Then
|
||||
verify(userRepository, never()).insert(user);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue