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> </build>
<properties> <properties>
<spring-boot.version>2.6.4</spring-boot.version>
<spring-framework.version>5.3.20</spring-framework.version> <spring-framework.version>5.3.20</spring-framework.version>
<!-- testing --> <!-- testing -->
<mockito.version>4.6.1</mockito.version> <mockito.version>4.6.1</mockito.version>

View File

@ -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,21 +26,28 @@ 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; private UserService userService;
SettingRepository settingRepository;
@Mock UserRepository userRepository;
@Mock MailClient mailClient;
User user; private SettingRepository settingRepository;
@Mock
private UserRepository userRepository;
@Mock
private MailClient mailClient;
private 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;
} }
@ -66,7 +73,7 @@ public class UserServiceUnitTest {
// 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);
} }
@ -79,8 +86,8 @@ public class UserServiceUnitTest {
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
@ -96,8 +103,8 @@ public class UserServiceUnitTest {
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
@ -115,8 +122,8 @@ public class UserServiceUnitTest {
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