Advertisement
andersonalmada2

Untitled

Aug 12th, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 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.beans.factory.annotation.Autowired;
  21. import org.springframework.boot.test.web.client.TestRestTemplate;
  22. import org.springframework.http.HttpStatus;
  23. import org.springframework.http.ResponseEntity;
  24.  
  25. import br.ufc.mandacaru5.model.Feedback;
  26. import br.ufc.mandacaru5.model.Product;
  27. import br.ufc.mandacaru5.service.ProductService;
  28.  
  29. public class ProductControllerTest {
  30.    
  31.     // Product
  32.     private static final int ID = 1;
  33.     private static final String NAME = "notebook";
  34.     private static final double PRICE = 450.0;
  35.  
  36.     // Feedback
  37.     private static final int ID_F1 = 1;
  38.     private static final int ID_F2 = 2;
  39.     private static final String MESSAGE_F1 = "muito rapido";
  40.     private static final String MESSAGE_F2 = "muito lento";
  41.  
  42.     @InjectMocks
  43.     private ProductController controller;
  44.  
  45.     @Mock
  46.     private ProductService service;
  47.  
  48.     private Product product;
  49.  
  50.     private List<Feedback> listFeedback;
  51.  
  52.     @Autowired
  53.     private TestRestTemplate restTemplate;
  54.  
  55.     private void start() {
  56.         Feedback feedback1 = new Feedback();
  57.         feedback1.setId(ID_F1);
  58.         feedback1.setMessage(MESSAGE_F1);
  59.  
  60.         Feedback feedback2 = new Feedback();
  61.         feedback2.setId(ID_F2);
  62.         feedback2.setMessage(MESSAGE_F2);
  63.  
  64.         listFeedback = new ArrayList<Feedback>();
  65.  
  66.         product = new Product(ID, NAME, PRICE, listFeedback);
  67.     }
  68.  
  69.     @BeforeEach
  70.     public void setUp() {
  71.         MockitoAnnotations.openMocks(this);
  72.         start();
  73.     }
  74.  
  75.     @Test
  76.     public void whenFindByIdThenReturnSuccess() {
  77.         when(service.find(anyInt())).thenReturn(product);
  78.  
  79.         ResponseEntity<Product> response = controller.find(ID);
  80.  
  81.         assertNotNull(response);
  82.         assertNotNull(response.getBody());
  83.         assertEquals(ResponseEntity.class, response.getClass());
  84.         assertEquals(Product.class, response.getBody().getClass());
  85.         assertEquals(ID, response.getBody().getId());
  86.         assertEquals(NAME, response.getBody().getName());
  87.         assertEquals(PRICE, response.getBody().getPrice());
  88.         assertEquals(listFeedback, response.getBody().getFeedbacks());
  89.     }
  90.  
  91.     @Test
  92.     public void whenFindAllThenReturnSuccess() {
  93.         when(service.findAll()).thenReturn(List.of(product));
  94.  
  95.         ResponseEntity<List<Product>> response = controller.findAll();
  96.  
  97.         assertNotNull(response);
  98.         assertNotNull(response.getBody());
  99.         assertEquals(ResponseEntity.class, response.getClass());
  100.         assertEquals(ID, response.getBody().get(0).getId());
  101.         assertEquals(NAME, response.getBody().get(0).getName());
  102.         assertEquals(PRICE, response.getBody().get(0).getPrice());
  103.         assertEquals(listFeedback, response.getBody().get(0).getFeedbacks());
  104.     }
  105.  
  106.     @Test
  107.     public void whenCreateThenReturnSuccess() {
  108.         doNothing().when(service).save(0, product);
  109.  
  110.         controller.save(product);
  111.  
  112.         verify(service).save(0, product);
  113.     }
  114.  
  115.     @Test
  116.     public void whenUpdateThenReturnSuccess() {
  117.         doNothing().when(service).save(ID, product);
  118.  
  119.         controller.update(ID, product);
  120.  
  121.         verify(service).save(ID, product);
  122.     }
  123.  
  124.     @Test
  125.     public void whenDeleteThenReturnSuccess() {
  126.         doNothing().when(service).delete(ID);
  127.  
  128.         controller.delete(ID);
  129.  
  130.         verify(service).delete(ID);
  131.     }
  132.  
  133.     @Test
  134.     public void whenFindByNameThenReturnSuccess() {
  135.         when(service.findByName(anyString())).thenReturn(product);
  136.  
  137.         ResponseEntity<Product> response = controller.findByName(NAME);
  138.  
  139.         assertNotNull(response);
  140.         assertNotNull(response.getBody());
  141.         assertEquals(ResponseEntity.class, response.getClass());
  142.         assertEquals(Product.class, response.getBody().getClass());
  143.         assertEquals(ID, response.getBody().getId());
  144.         assertEquals(NAME, response.getBody().getName());
  145.         assertEquals(PRICE, response.getBody().getPrice());
  146.         assertEquals(listFeedback, response.getBody().getFeedbacks());
  147.     }
  148.  
  149.     @Test
  150.     public void whenFindByNameThenReturnNotFound() {
  151.         when(service.findByName(anyString())).thenReturn(null);
  152.  
  153.         ResponseEntity<Product> response = controller.findByName(NAME);
  154.  
  155.         assertNotNull(response);
  156.         assertNull(response.getBody());
  157.         assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
  158.     }
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement