|
| 1 | +package com.baeldung.junit5.mockito; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.fail; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.never; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.Assertions; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.junit.platform.runner.JUnitPlatform; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.Mockito; |
| 18 | +import org.mockito.invocation.InvocationOnMock; |
| 19 | +import org.mockito.stubbing.Answer; |
| 20 | + |
| 21 | +import com.baeldung.junit5.mockito.repository.MailClient; |
| 22 | +import com.baeldung.junit5.mockito.repository.SettingRepository; |
| 23 | +import com.baeldung.junit5.mockito.repository.UserRepository; |
| 24 | +import com.baeldung.junit5.mockito.service.DefaultUserService; |
| 25 | +import com.baeldung.junit5.mockito.service.Errors; |
| 26 | +import com.baeldung.junit5.mockito.service.UserService; |
| 27 | + |
| 28 | +@RunWith(JUnitPlatform.class) |
| 29 | +@ExtendWith(MockitoExtension.class) |
| 30 | +public class UserServiceUnitTest { |
| 31 | + |
| 32 | + UserService userService; |
| 33 | + @Mock UserRepository userRepository; |
| 34 | + |
| 35 | + User user; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + void init(@Mock SettingRepository settingRepository, @Mock MailClient mailClient) { |
| 39 | + userService = new DefaultUserService(userRepository, settingRepository, mailClient); |
| 40 | + when(settingRepository.getUserMinAge()).thenReturn(10); |
| 41 | + when(settingRepository.getUserNameMinLength()).thenReturn(4); |
| 42 | + when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) { |
| 47 | + // Given |
| 48 | + user = new User("Jerry", 12); |
| 49 | + when(userRepository.insert(any(User.class))).then(new Answer<User>() { |
| 50 | + int sequence = 1; |
| 51 | + |
| 52 | + @Override |
| 53 | + public User answer(InvocationOnMock invocation) throws Throwable { |
| 54 | + User user = (User) invocation.getArgument(0); |
| 55 | + user.setId(sequence++); |
| 56 | + return user; |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + // When |
| 61 | + User insertedUser = userService.register(user); |
| 62 | + |
| 63 | + // Then |
| 64 | + verify(userRepository).insert(user); |
| 65 | + Assertions.assertNotNull(user.getId()); |
| 66 | + verify(mailClient).sendUserRegistrationMail(insertedUser); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void givenShortName_whenSaveUser_thenGiveShortUsernameError() { |
| 71 | + // Given |
| 72 | + user = new User("tom", 12); |
| 73 | + |
| 74 | + // When |
| 75 | + try { |
| 76 | + userService.register(user); |
| 77 | + fail("Should give an error"); |
| 78 | + } catch(Exception ex) { |
| 79 | + assertEquals(ex.getMessage(), Errors.USER_NAME_SHORT); |
| 80 | + } |
| 81 | + |
| 82 | + // Then |
| 83 | + verify(userRepository, never()).insert(user); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void givenSmallAge_whenSaveUser_thenGiveYoungUserError() { |
| 88 | + // Given |
| 89 | + user = new User("jerry", 3); |
| 90 | + |
| 91 | + // When |
| 92 | + try { |
| 93 | + userService.register(user); |
| 94 | + fail("Should give an error"); |
| 95 | + } catch(Exception ex) { |
| 96 | + assertEquals(ex.getMessage(), Errors.USER_AGE_YOUNG); |
| 97 | + } |
| 98 | + |
| 99 | + // Then |
| 100 | + verify(userRepository, never()).insert(user); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void givenUserWithExistingName_whenSaveUser_thenGiveUsernameAlreadyExistsError() { |
| 105 | + // Given |
| 106 | + user = new User("jerry", 12); |
| 107 | + Mockito.reset(userRepository); |
| 108 | + when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(true); |
| 109 | + |
| 110 | + // When |
| 111 | + try { |
| 112 | + userService.register(user); |
| 113 | + fail("Should give an error"); |
| 114 | + } catch(Exception ex) { |
| 115 | + assertEquals(ex.getMessage(), Errors.USER_NAME_DUPLICATE); |
| 116 | + } |
| 117 | + |
| 118 | + // Then |
| 119 | + verify(userRepository, never()).insert(user); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments