Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.team16.virtualwallet.services;
- import com.team16.virtualwallet.exceptions.DuplicateEntityException;
- import com.team16.virtualwallet.exceptions.EntityNotFoundException;
- import com.team16.virtualwallet.models.Card;
- import com.team16.virtualwallet.models.DTOs.CardDTO;
- import com.team16.virtualwallet.models.User;
- import com.team16.virtualwallet.models.mappers.CardMapper;
- import com.team16.virtualwallet.repositories.CardRepository;
- import com.team16.virtualwallet.repositories.UserRepository;
- import com.team16.virtualwallet.services.contracts.UserService;
- import org.junit.Assert;
- import org.junit.Test;
- import org.junit.jupiter.api.Assertions;
- import org.junit.runner.RunWith;
- import org.mockito.InjectMocks;
- import org.mockito.Mock;
- import org.mockito.Mockito;
- import org.mockito.junit.MockitoJUnitRunner;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
- import static com.team16.virtualwallet.Factory.*;
- import static org.mockito.ArgumentMatchers.*;
- import static org.mockito.Mockito.*;
- @RunWith(MockitoJUnitRunner.class)
- public class CardServiceImplTests {
- @Mock
- CardRepository mockCardRepository;
- @Mock
- UserRepository mockUserRepository;
- @Mock
- UserService mockUserService;
- @Mock
- CardMapper mockCardMapper;
- @InjectMocks
- CardServiceImpl mockCardService;
- @Test(expected = EntityNotFoundException.class)
- public void findByIdAndOwnerShouldThrow_WhenCardDoestNotExist() {
- //Act
- mockCardService.findByIdAndOwner(anyInt(), any(User.class));
- }
- @Test
- public void createCardShould_CallRepository() {
- CardDTO cardDto = createCardDto();
- User user = createUser();
- when(mockUserRepository.findByUsernameAndEnabledIsTrue(cardDto.getCardOwner())).thenReturn(Optional.of(user));
- mockCardService.create(cardDto);
- verify(mockCardRepository, times(1)).save(mockCardMapper.mapToCard(cardDto));
- }
- @Test
- public void updateCardShould_CallRepository() {
- Card card = createCard();
- CardDTO cardDto = createCardDto();
- when(mockCardRepository.findById(anyInt())).thenReturn(card);
- mockCardService.update(anyInt(), card.getCardNumber(), cardDto);
- verify(mockCardRepository, times(1)).save(card);
- }
- @Test
- public void findByIdShould_CallRepository() {
- //Arrange
- Card card = createCard();
- Mockito.when(mockCardRepository.findById(anyInt())).thenReturn(card);
- //Act
- mockCardService.findById(anyInt());
- //Assert
- Mockito.verify(mockCardRepository,
- times(1)).findById(anyInt());
- }
- @Test
- public void findAllByCardOwnerShould_CallRepository() {
- //Arrange
- User user = createUser();
- Mockito.when(mockCardRepository.findAllByCardOwnerAndDeletedIsFalse(user)).thenReturn(user.getUserCards());
- //Act
- mockCardService.findAllByOwner(user);
- //Assert
- Mockito.verify(mockCardRepository,
- times(1)).findAllByCardOwnerAndDeletedIsFalse(user);
- }
- @Test(expected = EntityNotFoundException.class)
- public void findByCardNumberAndCardNumberShouldThrow_EntityNotFoundException() {
- //Arrange
- Card card = createCard();
- User cardOwner = card.getCardOwner();
- when(mockCardService.findByCardNumberAndCardOwner(card.getCardNumber(), card.getCardOwner().getUsername())).thenReturn(card);
- //Act
- mockCardService.findByCardNumberAndCardOwner(card.getCardNumber(), card.getCardOwner().getUsername());
- //Assert
- verify(mockCardRepository, timeout(1)).findByCardNumberAndCardOwnerAndDeletedIsFalse(card.getCardNumber(), card.getCardOwner());
- }
- @Test(expected = EntityNotFoundException.class)
- public void deleteCardOwnerAndCardNumberShould_CallRepository() {
- //Arrange
- Card card = createCard();
- User owner = createUser();
- owner.getUserCards().add(card);
- card.setCardOwner(owner);
- when(mockUserRepository.findByUsernameAndEnabledIsTrue(anyString())).thenReturn(Optional.of(owner));
- when(mockCardRepository.findByCardNumberAndCardOwnerAndDeletedIsFalse(card.getCardNumber(),owner))
- .thenReturn(Optional.of(card));
- //Act
- mockCardService.deleteCardByCardOwnerAndCardNumber(card.getCardNumber(), owner.getUsername());
- //Assert
- verify(mockCardRepository, times(1)).save(card);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement