Advertisement
andersonalmada2

Untitled

Aug 15th, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 KB | None | 0 0
  1. package br.ufc.mandacaru5.controller;
  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.anyInt;
  7. import static org.mockito.ArgumentMatchers.anyString;
  8. import static org.mockito.Mockito.doNothing;
  9. import static org.mockito.Mockito.verify;
  10. import static org.mockito.Mockito.when;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. import org.junit.jupiter.api.BeforeEach;
  16. import org.junit.jupiter.api.Test;
  17. import org.mockito.InjectMocks;
  18. import org.mockito.Mock;
  19. import org.mockito.MockitoAnnotations;
  20. import org.springframework.http.HttpStatus;
  21. import org.springframework.http.ResponseEntity;
  22.  
  23. import br.ufc.mandacaru5.model.Feedback;
  24. import br.ufc.mandacaru5.model.Product;
  25. import br.ufc.mandacaru5.service.ProductService;
  26.  
  27. public class ProductControllerTest {
  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 ProductController controller;
  42.  
  43.     @Mock
  44.     private ProductService service;
  45.  
  46.     private Product product;
  47.     private List<Feedback> listFeedback;
  48.  
  49.     private void start() {
  50.         Feedback feedback1 = new Feedback();
  51.         feedback1.setId(ID_F1);
  52.         feedback1.setMessage(MESSAGE_F1);
  53.  
  54.         Feedback feedback2 = new Feedback();
  55.         feedback2.setId(ID_F2);
  56.         feedback2.setMessage(MESSAGE_F2);
  57.  
  58.         listFeedback = new ArrayList<Feedback>();
  59.         listFeedback.add(feedback1);
  60.         listFeedback.add(feedback2);
  61.  
  62.         product = new Product(ID, NAME, PRICE, listFeedback);
  63.     }
  64.  
  65.     @BeforeEach
  66.     public void setUp() {
  67.         MockitoAnnotations.openMocks(this);
  68.         start();
  69.     }
  70.  
  71.     @Test
  72.     public void whenFindByIdThenReturnSuccess() {
  73.         when(service.find(anyInt())).thenReturn(product);
  74.  
  75.         ResponseEntity<Product> response = controller.find(ID);
  76.  
  77.         assertNotNull(response);
  78.         assertNotNull(response.getBody());
  79.         assertEquals(ResponseEntity.class, response.getClass());
  80.         assertEquals(Product.class, response.getBody().getClass());
  81.         assertEquals(ID, response.getBody().getId());
  82.         assertEquals(NAME, response.getBody().getName());
  83.         assertEquals(PRICE, response.getBody().getPrice());
  84.         assertEquals(listFeedback, response.getBody().getFeedbacks());
  85.     }
  86.  
  87.     @Test
  88.     public void whenFindByIdThenReturnNotFound() {
  89.         when(service.find(anyInt())).thenReturn(null);
  90.  
  91.         ResponseEntity<Product> response = controller.find(ID);
  92.  
  93.         assertNotNull(response);
  94.         assertNull(response.getBody());
  95.         assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
  96.     }
  97.  
  98.     @Test
  99.     public void whenFindAllThenReturnSuccess() {
  100.         when(service.findAll()).thenReturn(List.of(product));
  101.  
  102.         ResponseEntity<List<Product>> response = controller.findAll();
  103.  
  104.         assertNotNull(response);
  105.         assertNotNull(response.getBody());
  106.         assertEquals(ResponseEntity.class, response.getClass());
  107.         assertEquals(ID, response.getBody().get(0).getId());
  108.         assertEquals(NAME, response.getBody().get(0).getName());
  109.         assertEquals(PRICE, response.getBody().get(0).getPrice());
  110.         assertEquals(listFeedback, response.getBody().get(0).getFeedbacks());
  111.     }
  112.  
  113.     @Test
  114.     public void whenCreateThenReturnSuccess() {
  115.         doNothing().when(service).save(0, product);
  116.  
  117.         controller.save(product);
  118.  
  119.         verify(service).save(0, product);
  120.     }
  121.  
  122.     @Test
  123.     public void whenUpdateThenReturnSuccess() {
  124.         doNothing().when(service).save(ID, product);
  125.  
  126.         controller.update(ID, product);
  127.  
  128.         verify(service).save(ID, product);
  129.     }
  130.  
  131.     @Test
  132.     public void whenDeleteThenReturnSuccess() {
  133.         doNothing().when(service).delete(ID);
  134.  
  135.         controller.delete(ID);
  136.  
  137.         verify(service).delete(ID);
  138.     }
  139.  
  140.     @Test
  141.     public void whenFindByNameThenReturnSuccess() {
  142.         when(service.findByName(anyString())).thenReturn(product);
  143.  
  144.         ResponseEntity<Product> response = controller.findByName(NAME);
  145.  
  146.         assertNotNull(response);
  147.         assertNotNull(response.getBody());
  148.         assertEquals(ResponseEntity.class, response.getClass());
  149.         assertEquals(Product.class, response.getBody().getClass());
  150.         assertEquals(ID, response.getBody().getId());
  151.         assertEquals(NAME, response.getBody().getName());
  152.         assertEquals(PRICE, response.getBody().getPrice());
  153.         assertEquals(listFeedback, response.getBody().getFeedbacks());
  154.     }
  155.  
  156.     @Test
  157.     public void whenFindByNameThenReturnNotFound() {
  158.         when(service.findByName(anyString())).thenReturn(null);
  159.  
  160.         ResponseEntity<Product> response = controller.findByName(NAME);
  161.  
  162.         assertNotNull(response);
  163.         assertNull(response.getBody());
  164.         assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
  165.     }
  166.  
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement