SHARE
TWEET

Untitled

a guest Sep 16th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ru.kirkazan.rmis.app.stocks.impl.service;
  2.  
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.mockito.InjectMocks;
  7. import org.mockito.Mock;
  8. import org.mockito.runners.MockitoJUnitRunner;
  9. import ru.kirkazan.common.exception.CodifiedException;
  10. import ru.kirkazan.rmis.app.organization.model.OrganizationCode;
  11. import ru.kirkazan.rmis.app.organization.service.OrganizationService;
  12. import ru.kirkazan.rmis.app.stocks.api.model.Clinic;
  13. import ru.kirkazan.rmis.app.stocks.impl.entity.ClinicEntity;
  14.  
  15. import javax.persistence.EntityManager;
  16. import javax.persistence.Query;
  17. import javax.persistence.TypedQuery;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. import static org.mockito.Matchers.any;
  23. import static org.mockito.Matchers.anyInt;
  24. import static org.mockito.Matchers.anyString;
  25. import static org.mockito.Mockito.mock;
  26. import static org.mockito.Mockito.verify;
  27. import static org.mockito.Mockito.when;
  28.  
  29. @RunWith(MockitoJUnitRunner.class)
  30. public class TestClinicService {
  31.  
  32.     @InjectMocks
  33.     private ClinicServiceImpl clinicService;
  34.  
  35.     @Mock
  36.     private EntityManager entityManager;
  37.  
  38.     @Mock
  39.     private OrganizationService organizationService;
  40.  
  41.     private static final Integer CLINIC_ID = 1;
  42.  
  43.     @Before
  44.     public void setUp() throws Exception {
  45.         ClinicEntity clinicEntity = new ClinicEntity();
  46.         clinicEntity.setId(CLINIC_ID);
  47.         when(entityManager.getReference(ClinicEntity.class, CLINIC_ID)).thenReturn(clinicEntity);
  48.         TypedQuery mockQuery = mock(TypedQuery.class);
  49.         when(entityManager.createQuery(anyString(), any())).thenReturn(mockQuery);
  50.         when(mockQuery.setParameter(anyString(), any())).thenReturn(mockQuery);
  51.  
  52.     }
  53.  
  54.     @Test
  55.     public void testSaveNewClinic() throws Exception {
  56.         Clinic newClinic = getClinic();
  57.         clinicService.saveClinic(newClinic);
  58.         verify(entityManager).persist(any(ClinicEntity.class));
  59.         verify(organizationService).save(newClinic);
  60.     }
  61.  
  62.     @Test
  63.     public void testSaveExistedClinic() throws Exception {
  64.         Clinic clinic = getClinic();
  65.         clinic.setId(CLINIC_ID);
  66.         clinicService.saveClinic(clinic);
  67.         verify(entityManager).getReference(ClinicEntity.class, clinic.getId());
  68.         verify(organizationService).save(clinic);
  69.     }
  70.  
  71.     @Test(expected = CodifiedException.class)
  72.     public void testSaveSameClinic() throws Exception {
  73.         List<Clinic> clinics=new ArrayList<>();
  74.         clinics.add(getClinic());
  75.         TypedQuery mockQuery = mock(TypedQuery.class);
  76.         when(entityManager.createQuery(anyString(), any())).thenReturn(mockQuery);
  77.         when(mockQuery.getResultList()).thenReturn(clinics);
  78.         when(mockQuery.setParameter(anyString(), any())).thenReturn(mockQuery);
  79.         Clinic clinic = getClinic();
  80.         clinicService.saveClinic(clinic);
  81.     }
  82.  
  83.     private Clinic getClinic() {
  84.         Clinic clinic = new Clinic();
  85.         clinic.setFullName("testFullName");
  86.         clinic.setShortName("testShortName");
  87.         clinic.setCode("testCode");
  88.         return clinic;
  89.     }
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top