|
| 1 | +package com.baeldung.jpaguide; |
| 2 | + |
| 3 | +import com.baeldung.spring.jpa.guide.model.Publishers; |
| 4 | +import com.baeldung.spring.jpa.guide.repository.PublisherRepository; |
| 5 | +import com.baeldung.spring.jpa.guide.service.PublisherService; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Optional; |
| 11 | + |
| 12 | +import static org.junit.Assert.assertEquals; |
| 13 | +import static org.junit.Assert.assertNotNull; |
| 14 | +import static org.mockito.Mockito.mock; |
| 15 | +import static org.mockito.Mockito.spy; |
| 16 | +import static org.mockito.Mockito.times; |
| 17 | +import static org.mockito.Mockito.verify; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | +import static org.mockito.ArgumentMatchers.anyString; |
| 22 | + |
| 23 | +public class JpaGuideUnitTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void givenPublisher_whenSave_thenPublisherIsSaved() { |
| 27 | + PublisherRepository publisherRepository = mock(PublisherRepository.class); |
| 28 | + PublisherService publisherService = spy(new PublisherService(publisherRepository)); |
| 29 | + Publishers publishers = new Publishers("Springer", "Dallas", 10); |
| 30 | + when(publisherRepository.save(any(Publishers.class))).thenReturn(publishers); |
| 31 | + publisherService.save(publishers); |
| 32 | + verify(publisherRepository, times(1)).save(any(Publishers.class)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenPublishers_whenFindAll_thenReturnAllPublishers() { |
| 37 | + PublisherRepository publisherRepository = mock(PublisherRepository.class); |
| 38 | + PublisherService publisherService = new PublisherService(publisherRepository); |
| 39 | + List<Publishers> publishersList = Arrays.asList(new Publishers("Springer", "Dallas", 10), new Publishers("O'Reilly", "San Francisco", 20)); |
| 40 | + when(publisherRepository.findAll()).thenReturn(publishersList); |
| 41 | + |
| 42 | + List<Publishers> result = publisherService.findAll(); |
| 43 | + |
| 44 | + assertEquals(2, result.size()); |
| 45 | + verify(publisherRepository, times(1)).findAll(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenPublisherId_whenFindById_thenReturnPublisher() { |
| 50 | + PublisherRepository publisherRepository = mock(PublisherRepository.class); |
| 51 | + PublisherService publisherService = new PublisherService(publisherRepository); |
| 52 | + Publishers publisher = new Publishers("Springer", "Dallas", 10); |
| 53 | + when(publisherRepository.findById(anyInt())).thenReturn(Optional.of(publisher)); |
| 54 | + |
| 55 | + Publishers result = publisherService.findById(1); |
| 56 | + |
| 57 | + assertNotNull(result); |
| 58 | + verify(publisherRepository, times(1)).findById(anyInt()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void givenPublisher_whenUpdate_thenPublisherIsUpdated() { |
| 63 | + PublisherRepository publisherRepository = mock(PublisherRepository.class); |
| 64 | + PublisherService publisherService = new PublisherService(publisherRepository); |
| 65 | + Publishers publisher = new Publishers("Springer", "Dallas", 10); |
| 66 | + when(publisherRepository.save(any(Publishers.class))).thenReturn(publisher); |
| 67 | + |
| 68 | + Publishers result = publisherService.update(publisher); |
| 69 | + |
| 70 | + assertNotNull(result); |
| 71 | + verify(publisherRepository, times(1)).save(any(Publishers.class)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void givenLocation_whenFindAllByLocation_thenReturnPublishersInLocation() { |
| 76 | + PublisherRepository publisherRepository = mock(PublisherRepository.class); |
| 77 | + PublisherService publisherService = new PublisherService(publisherRepository); |
| 78 | + List<Publishers> publishersList = Arrays.asList(new Publishers("Springer", "Dallas", 10), new Publishers("O'Reilly", "Dallas", 20)); |
| 79 | + when(publisherRepository.findAllByLocation(anyString())).thenReturn(publishersList); |
| 80 | + |
| 81 | + List<Publishers> result = publisherService.findAllByLocation("Dallas"); |
| 82 | + |
| 83 | + assertEquals(2, result.size()); |
| 84 | + verify(publisherRepository, times(1)).findAllByLocation(anyString()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void givenMinJournalsAndLocation_whenFindPublishersWithMinJournalsInLocation_thenReturnPublishers() { |
| 89 | + PublisherRepository publisherRepository = mock(PublisherRepository.class); |
| 90 | + PublisherService publisherService = new PublisherService(publisherRepository); |
| 91 | + List<Publishers> publishersList = Arrays.asList(new Publishers("Springer", "Dallas", 10), new Publishers("O'Reilly", "Dallas", 20)); |
| 92 | + when(publisherRepository.findPublishersWithMinJournalsInLocation(anyInt(), anyString())).thenReturn(publishersList); |
| 93 | + |
| 94 | + List<Publishers> result = publisherService.findPublishersWithMinJournalsInLocation(5, "Dallas"); |
| 95 | + |
| 96 | + assertEquals(2, result.size()); |
| 97 | + verify(publisherRepository, times(1)).findPublishersWithMinJournalsInLocation(anyInt(), anyString()); |
| 98 | + } |
| 99 | +} |
0 commit comments