Advertisement
andersonalmada2

Untitled

Aug 11th, 2022
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. package br.ufc.mandacaru5.service;
  2.  
  3. import static org.junit.jupiter.api.Assertions.assertEquals;
  4. import static org.junit.jupiter.api.Assertions.assertNotNull;
  5. import static org.junit.jupiter.api.Assertions.assertNull;
  6. import static org.mockito.ArgumentMatchers.any;
  7. import static org.mockito.ArgumentMatchers.anyInt;
  8. import static org.mockito.ArgumentMatchers.anyString;
  9. import static org.mockito.Mockito.doNothing;
  10. import static org.mockito.Mockito.verify;
  11. import static org.mockito.Mockito.when;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Optional;
  16.  
  17. import org.junit.jupiter.api.BeforeEach;
  18. import org.junit.jupiter.api.Test;
  19. import org.mockito.InjectMocks;
  20. import org.mockito.Mock;
  21. import org.mockito.MockitoAnnotations;
  22.  
  23. import br.ufc.mandacaru5.model.Feedback;
  24. import br.ufc.mandacaru5.model.Product;
  25. import br.ufc.mandacaru5.repository.ProductRepository;
  26.  
  27. public class ProductServiceTest {
  28.  
  29.     // Product
  30.     private static final int ID = 1;
  31.     private static final String NAME = "notebook";
  32.     private static final double PRICE = 450.0;
  33.  
  34.     // Feedback
  35.     private static final int ID_F1 = 1;
  36.     private static final int ID_F2 = 2;
  37.     private static final String MESSAGE_F1 = "muito rapido";
  38.     private static final String MESSAGE_F2 = "muito lento";
  39.  
  40.     @InjectMocks
  41.     private ProductService service;
  42.  
  43.     @Mock
  44.     private ProductRepository repository;
  45.  
  46.     private Product product;
  47.  
  48.     private List<Feedback> listFeedback;
  49.  
  50.     private void start() {
  51.         Feedback feedback1 = new Feedback();
  52.         feedback1.setId(ID_F1);
  53.         feedback1.setMessage(MESSAGE_F1);
  54.  
  55.         Feedback feedback2 = new Feedback();
  56.         feedback2.setId(ID_F2);
  57.         feedback2.setMessage(MESSAGE_F2);
  58.  
  59.         listFeedback = new ArrayList<Feedback>();
  60.  
  61.         product = new Product(ID, NAME, PRICE, listFeedback);
  62.     }
  63.  
  64.     @BeforeEach
  65.     public void setUp() {
  66.         MockitoAnnotations.openMocks(this);
  67.         start();
  68.     }
  69.  
  70.     @Test
  71.     public void whenFindByIdThenReturnAProduct() {
  72.         when(repository.findById(anyInt())).thenReturn(Optional.of(product));
  73.  
  74.         Product response = service.find(ID);
  75.  
  76.         assertNotNull(response);
  77.         assertEquals(Product.class, response.getClass());
  78.         assertEquals(ID, response.getId());
  79.         assertEquals(NAME, response.getName());
  80.         assertEquals(PRICE, response.getPrice());
  81.         assertEquals(listFeedback, response.getFeedbacks());
  82.     }
  83.  
  84.     @Test
  85.     public void whenFindByIdThenReturnNullIfIdLessThan1() {
  86.         Product response = service.find(0);
  87.         assertNull(response);
  88.     }
  89.  
  90.     @Test
  91.     public void whenFindByIdThenReturnNullIfOptionalNotPresent() {
  92.         when(repository.findById(anyInt())).thenReturn(Optional.empty());
  93.  
  94.         Product response = service.find(ID);
  95.         assertNull(response);
  96.     }
  97.  
  98.     @Test
  99.     public void whenFindAllThenReturnAnList() {
  100.         when(repository.findAll()).thenReturn(List.of(product));
  101.  
  102.         List<Product> response = service.findAll();
  103.  
  104.         assertNotNull(response);
  105.         assertEquals(ID, response.get(0).getId());
  106.         assertEquals(NAME, response.get(0).getName());
  107.         assertEquals(PRICE, response.get(0).getPrice());
  108.         assertEquals(listFeedback, response.get(0).getFeedbacks());
  109.     }
  110.  
  111.     @Test
  112.     public void whenSaveVerifySuccess() {
  113.         when(repository.save(any())).thenReturn(product);
  114.  
  115.         service.save(0, product);
  116.  
  117.         verify(repository).save(any());
  118.     }
  119.  
  120.     @Test
  121.     public void whenUpdateVerifySuccess() {
  122.         when(repository.save(any())).thenReturn(product);
  123.        
  124.         service.save(ID, product);
  125.  
  126.         verify(repository).save(any());
  127.     }
  128.    
  129.     @Test
  130.     public void whenDeleteVerifySuccess() {
  131.         when(repository.findById(anyInt())).thenReturn(Optional.of(product));
  132.         doNothing().when(repository).delete(product);
  133.        
  134.         service.delete(ID);
  135.  
  136.         verify(repository).findById(anyInt());
  137.         verify(repository).delete(any());
  138.     }
  139.    
  140.     @Test
  141.     public void whenFindByNameThenReturnAProduct() {
  142.         when(repository.findFirstByName(anyString())).thenReturn(product);
  143.  
  144.         Product response = service.findByName(NAME);
  145.        
  146.         assertNotNull(response);
  147.         assertEquals(NAME, response.getName());
  148.     }
  149.    
  150.     @Test
  151.     public void whenFindByNameThenReturnNullIfNameLess3() {
  152.         when(repository.findFirstByName(anyString())).thenReturn(null);
  153.  
  154.         Product response = service.findByName("as");
  155.        
  156.         assertNull(response);
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement