BAEL-2201-shreyas-Minor changes to reflect the code used in article.

This commit is contained in:
Shreyas Mahajan 2018-11-02 14:55:16 +05:30
parent adacecbd75
commit 9604defd6c
1 changed files with 9 additions and 2 deletions

View File

@ -9,6 +9,8 @@ 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;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
@ -23,9 +25,11 @@ 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)
@RunWith(JUnitPlatform.class)
public class UserServiceUnitTest { public class UserServiceUnitTest {
UserService userService; UserService userService;
SettingRepository settingRepository;
@Mock UserRepository userRepository; @Mock UserRepository userRepository;
@Mock MailClient mailClient; @Mock MailClient mailClient;
@ -37,10 +41,11 @@ public class UserServiceUnitTest {
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;
} }
@Test @Test
void givenValidUser_whenSaveUser_thenSucceed() { 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>() {
@ -54,6 +59,8 @@ public class UserServiceUnitTest {
} }
}); });
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
// When // When
User insertedUser = userService.register(user); User insertedUser = userService.register(user);