-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests for user service (find user by username) and contact servi…
…ce (find contacts by username) written along with some other minor refactoring.
- Loading branch information
Badri Paudel
committed
Mar 16, 2024
1 parent
d09c8cb
commit bf5caa6
Showing
7 changed files
with
206 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package info.keeper.service; | ||
|
||
import info.keeper.models.User; | ||
import info.keeper.repositories.UserRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserService { | ||
private UserRepository userRepository; | ||
|
||
public UserService(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
public User getUserByUsername(String username) { | ||
User user = userRepository.findUserByUsername(username); | ||
return user; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package info.keeper.service; | ||
|
||
import info.keeper.models.Contact; | ||
import info.keeper.models.User; | ||
import info.keeper.repositories.ContactRepository; | ||
import info.keeper.repositories.UserRepository; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@SpringBootTest | ||
class ContactServiceTest { | ||
@Mock | ||
private ContactRepository contactRepository; | ||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@InjectMocks | ||
private ContactService serviceUnderTest; | ||
|
||
private User user; | ||
private Integer userId; | ||
private String username; | ||
private Integer pageNumber; | ||
private Long expectedNumberOfContacts; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
// Any setup before each test. | ||
username = "john.doe@gmail.com"; | ||
userId = 101; | ||
pageNumber = 1; | ||
expectedNumberOfContacts = 5L; | ||
user = new User(); | ||
user.setEmail(username); | ||
user.setId(userId); | ||
} | ||
@AfterEach | ||
void cleanUp() { | ||
// Any cleanup after each test. | ||
} | ||
|
||
@Test | ||
void findContactsByUser() { | ||
Mockito.when(userRepository.findUserByUsername(username)).thenReturn(user); | ||
Pageable pageable = PageRequest.of(pageNumber, 5); | ||
Mockito.when(contactRepository.findContactsByUser(userId, pageable)).thenReturn(getContactsWithPaginationForUser()); | ||
Page<Contact> allContacts = serviceUnderTest.findContactsByUser(username, pageNumber); | ||
assertThat(allContacts.getTotalElements()).isEqualTo(expectedNumberOfContacts); | ||
Assertions.assertAll("Assert that returned elements have My Contact Name in name and 123456789 in phone number", | ||
() -> { | ||
allContacts.getContent().forEach( contact -> { | ||
Assertions.assertTrue(contact.getName().contains("My Contact Name") && | ||
contact.getPhoneNumber().contains("123456789")); | ||
}); | ||
} | ||
); | ||
} | ||
|
||
private Page<Contact> getContactsWithPaginationForUser() { | ||
List<Contact> contacts = new ArrayList<>(); | ||
for (int i = 1; i <= 5; i++) { | ||
String contactName = "My Contact Name - " + i; | ||
String phoneNumber = "123456789" + i; | ||
Contact contact = new Contact(); | ||
contact.setId(i); | ||
contact.setName(contactName); | ||
contact.setPhoneNumber(phoneNumber); | ||
contacts.add(contact); | ||
} | ||
// Create a mock Page object containing the contacts | ||
PageImpl<Contact> pagedContacts = new PageImpl<>(contacts); | ||
return pagedContacts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package info.keeper.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.*; | ||
|
||
import info.keeper.models.User; | ||
import info.keeper.repositories.UserRepository; | ||
import org.junit.jupiter.api.*; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
/** | ||
* REF: https://www.baeldung.com/java-spring-mockito-mock-mockbean (Mockito.Mock vs @Mock syntax) | ||
* REF : https://stackoverflow.com/questions/16467685/difference-between-mock-and-injectmocks (@InjectMocks and @Mock) | ||
*/ | ||
@SpringBootTest | ||
class UserServiceTest { | ||
// This UserRepository will be mocked (for some method of it, we will be returning the result of the mock and not the real db call) | ||
@Mock | ||
private UserRepository userRepository; | ||
|
||
/** | ||
* Mark a field on which injection should be performed. | ||
* @InjectMocks creates the instance of the class and injects the mocks that are | ||
* created with the @Mock (or @Spy) annotations into this instance. | ||
* REF: https://stackoverflow.com/questions/16467685/difference-between-mock-and-injectmocks | ||
*/ | ||
@InjectMocks | ||
private UserService userService; // Service under test, all services ( or repo ..) with Mock annotations applied will be injected into it. | ||
|
||
// Define fields. | ||
private static User user; | ||
private static String username; | ||
|
||
@BeforeAll | ||
static void setUpData() { | ||
username = "badripaudel77@gmail.com"; | ||
user = new User(); | ||
user.setEmail(username); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
// Any setup before each test. | ||
} | ||
@AfterEach | ||
void cleanUp() { | ||
// Any cleanup after each test. | ||
} | ||
|
||
@Test | ||
@DisplayName("Test that getUserByUsername() of the service returns correct user.") | ||
void testGetUserByUsername() { | ||
/** | ||
* Define mock behavior for userRepository | ||
* That is, when user userRepository.findUserByUsername() is called, return the user (mock it, instead of calling the db) | ||
*/ | ||
when(userRepository.findUserByUsername(username)).thenReturn(user); | ||
// Mockito.when(userRepository.findUserByUsername(username)).thenReturn(user); | ||
// Call the method under test | ||
User userByUsername = userService.getUserByUsername(username); | ||
|
||
// Verify that the userRepository method was called with the correct parameter | ||
verify(userRepository).findUserByUsername("badripaudel77@gmail.com"); | ||
|
||
// Verify that the returned user is the same as the expected user | ||
assertThat(userByUsername).isEqualTo(user); | ||
assert userByUsername.getEmail().equals(user.getEmail()) == true; | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() { | ||
// Any cleanup after all the tests are executed. | ||
} | ||
} |