spacerose

Untitled

Sep 26th, 2021
947
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.demo;
  2.  
  3. import com.example.demo.DAO.PhoneDAO;
  4. import com.example.demo.models.Phone;
  5. import com.example.demo.repos.PhoneRepository;
  6. import org.junit.jupiter.api.Assertions;
  7. import org.junit.jupiter.api.Test;
  8. import org.junit.jupiter.api.extension.ExtendWith;
  9. import org.mockito.ArgumentCaptor;
  10. import org.mockito.Captor;
  11. import org.mockito.Mock;
  12. import org.mockito.Mockito;
  13. import org.mockito.junit.jupiter.MockitoExtension;
  14.  
  15. import java.util.List;
  16.  
  17. @ExtendWith(MockitoExtension.class)
  18. public class PhoneDAOTest {
  19.     @Mock
  20.     private PhoneRepository phoneRepo;
  21.     @Captor
  22.     ArgumentCaptor<Phone> captor;
  23.     @Test
  24.     void getPhones() {
  25.         Phone phone1 = new Phone();
  26.         phone1.setName("IPhone XS");
  27.         Phone phone2 = new Phone();
  28.         phone2.setName("Samsung Galaxy A71");
  29.         Mockito.when(phoneRepo.findAll()).thenReturn(List.of(phone1,phone2));
  30.         PhoneDAO phoneDAO = new PhoneDAO(phoneRepo);
  31.  
  32.         Assertions.assertEquals(2,phoneDAO.getPhones().size());
  33.         Assertions.assertEquals("Samsung Galaxy A71",phoneDAO.getPhones().get(1).getName());
  34.  
  35.     }
  36.     @Test
  37.     void saveOrUpdate() {
  38.         Phone phone = new Phone();
  39.         phone.setName("IPhone 12 Pro");
  40.         PhoneDAO phoneDAO = new PhoneDAO(phoneRepo);
  41.  
  42.         phoneDAO.save(phone);
  43.         Mockito.verify(phoneRepo).save(captor.capture());
  44.         Phone captured = captor.getValue();
  45.         Assertions.assertEquals("IPhone 12 Pro", captured.getName());
  46.  
  47.     }
  48.  
  49.     @Test
  50.     void test(){
  51.         int x= 7;
  52.         int y=3;
  53.         Assertions.assertEquals(10,x+y);
  54.     }
  55. }
  56.  
RAW Paste Data