JAVA-13636 Updated Junit5 article
This commit is contained in:
parent
f798bc1d82
commit
bf888fc637
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.junit5.mockito;
|
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.junit.jupiter.api.Assertions.fail;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.lenient;
|
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.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
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;
|
import com.baeldung.junit5.mockito.service.UserService;
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
public class UserServiceUnitTest {
|
class UserServiceUnitTest {
|
||||||
|
|
||||||
UserService userService;
|
UserService userService;
|
||||||
|
|
||||||
SettingRepository settingRepository;
|
SettingRepository settingRepository;
|
||||||
@Mock UserRepository userRepository;
|
|
||||||
@Mock MailClient mailClient;
|
@Mock
|
||||||
|
UserRepository userRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
MailClient mailClient;
|
||||||
|
|
||||||
User user;
|
User user;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void init(@Mock SettingRepository settingRepository) {
|
void init(@Mock SettingRepository settingRepository) {
|
||||||
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
||||||
lenient().when(settingRepository.getUserMinAge()).thenReturn(10);
|
lenient().when(settingRepository.getUserMinAge())
|
||||||
|
.thenReturn(10);
|
||||||
when(settingRepository.getUserNameMinLength()).thenReturn(4);
|
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;
|
this.settingRepository = settingRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) {
|
void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) {
|
||||||
// Given
|
// Given
|
||||||
user = new User("Jerry", 12);
|
user = new User("Jerry", 12);
|
||||||
when(userRepository.insert(any(User.class))).then(new Answer<User>() {
|
when(userRepository.insert(any(User.class))).then(new Answer<User>() {
|
||||||
int sequence = 1;
|
int sequence = 1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User answer(InvocationOnMock invocation) throws Throwable {
|
public User answer(InvocationOnMock invocation) throws Throwable {
|
||||||
User user = (User) invocation.getArgument(0);
|
User user = (User) invocation.getArgument(0);
|
||||||
|
@ -63,10 +70,10 @@ public class UserServiceUnitTest {
|
||||||
|
|
||||||
// When
|
// When
|
||||||
User insertedUser = userService.register(user);
|
User insertedUser = userService.register(user);
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
verify(userRepository).insert(user);
|
verify(userRepository).insert(user);
|
||||||
Assertions.assertNotNull(user.getId());
|
assertNotNull(user.getId());
|
||||||
verify(mailClient).sendUserRegistrationMail(insertedUser);
|
verify(mailClient).sendUserRegistrationMail(insertedUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,53 +81,53 @@ public class UserServiceUnitTest {
|
||||||
void givenShortName_whenSaveUser_thenGiveShortUsernameError() {
|
void givenShortName_whenSaveUser_thenGiveShortUsernameError() {
|
||||||
// Given
|
// Given
|
||||||
user = new User("tom", 12);
|
user = new User("tom", 12);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
try {
|
try {
|
||||||
userService.register(user);
|
userService.register(user);
|
||||||
fail("Should give an error");
|
fail("Should give an error");
|
||||||
} catch(Exception ex) {
|
} catch (Exception ex) {
|
||||||
assertEquals(ex.getMessage(), Errors.USER_NAME_SHORT);
|
assertThat(ex.getMessage()).isEqualTo(Errors.USER_NAME_SHORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
verify(userRepository, never()).insert(user);
|
verify(userRepository, never()).insert(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenSmallAge_whenSaveUser_thenGiveYoungUserError() {
|
void givenSmallAge_whenSaveUser_thenGiveYoungUserError() {
|
||||||
// Given
|
// Given
|
||||||
user = new User("jerry", 3);
|
user = new User("jerry", 3);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
try {
|
try {
|
||||||
userService.register(user);
|
userService.register(user);
|
||||||
fail("Should give an error");
|
fail("Should give an error");
|
||||||
} catch(Exception ex) {
|
} catch (Exception ex) {
|
||||||
assertEquals(ex.getMessage(), Errors.USER_AGE_YOUNG);
|
assertThat(ex.getMessage()).isEqualTo(Errors.USER_AGE_YOUNG);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
verify(userRepository, never()).insert(user);
|
verify(userRepository, never()).insert(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenUserWithExistingName_whenSaveUser_thenGiveUsernameAlreadyExistsError() {
|
void givenUserWithExistingName_whenSaveUser_thenGiveUsernameAlreadyExistsError() {
|
||||||
// Given
|
// Given
|
||||||
user = new User("jerry", 12);
|
user = new User("jerry", 12);
|
||||||
Mockito.reset(userRepository);
|
Mockito.reset(userRepository);
|
||||||
when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(true);
|
when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(true);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
try {
|
try {
|
||||||
userService.register(user);
|
userService.register(user);
|
||||||
fail("Should give an error");
|
fail("Should give an error");
|
||||||
} catch(Exception ex) {
|
} catch (Exception ex) {
|
||||||
assertEquals(ex.getMessage(), Errors.USER_NAME_DUPLICATE);
|
assertThat(ex.getMessage()).isEqualTo(Errors.USER_NAME_DUPLICATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
verify(userRepository, never()).insert(user);
|
verify(userRepository, never()).insert(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue