From 9da5fd8a7a8ae3d4f8fff8aea2d5349f1fc3ca2a Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 14 May 2019 00:02:30 +0400 Subject: [PATCH] #104 shock, the first unit test works --- .../conference/service/ConferenceService.java | 2 + .../module_test/ConferenceServiceTest.java | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/test/java/conference/module_test/ConferenceServiceTest.java diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 38de435..8ffbf70 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -1,6 +1,7 @@ package ru.ulstu.conference.service; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -43,6 +44,7 @@ public class ConferenceService { private final ConferenceNotificationService conferenceNotificationService; private final EventService eventService; + @Autowired public ConferenceService(ConferenceRepository conferenceRepository, ConferenceUserService conferenceUserService, DeadlineService deadlineService, diff --git a/src/test/java/conference/module_test/ConferenceServiceTest.java b/src/test/java/conference/module_test/ConferenceServiceTest.java new file mode 100644 index 0000000..44c92ea --- /dev/null +++ b/src/test/java/conference/module_test/ConferenceServiceTest.java @@ -0,0 +1,37 @@ +package conference.module_test; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.domain.Sort; +import ru.ulstu.conference.model.Conference; +import ru.ulstu.conference.repository.ConferenceRepository; +import ru.ulstu.conference.service.ConferenceService; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +@RunWith(MockitoJUnitRunner.class) +public class ConferenceServiceTest { + + @Mock + ConferenceRepository conferenceRepository; + + @InjectMocks + ConferenceService conferenceService; + + @Test + public void testFindAllConferences() { + List conferences = new ArrayList<>(); + Conference conference = new Conference(); + conference.setId(1); + conferences.add(conference); + Mockito.when(conferenceRepository.findAll(new Sort(Sort.Direction.DESC, "beginDate"))).thenReturn(conferences); + assertEquals(conferences, conferenceService.findAll()); + } +}