Skip to content
Snippets Groups Projects
Commit 44df5327 authored by Dominika Zemanovičová's avatar Dominika Zemanovičová
Browse files

Fix ExerciseServiceTest

parent 63cbe6fc
No related branches found
No related tags found
3 merge requests!31M2,!30M2 exercise,!29M2 exercise
Pipeline #
...@@ -2,9 +2,6 @@ package org.fuseri.moduleexercise.exercise; ...@@ -2,9 +2,6 @@ package org.fuseri.moduleexercise.exercise;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import org.fuseri.model.dto.exercise.ExerciseCreateDto; import org.fuseri.model.dto.exercise.ExerciseCreateDto;
//import org.fuseri.modulelanguageschool.common.ResourceNotFoundException;
import org.fuseri.model.dto.exercise.ExerciseDto;
import org.fuseri.moduleexercise.question.Question; import org.fuseri.moduleexercise.question.Question;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
...@@ -27,13 +24,13 @@ import static org.mockito.Mockito.when; ...@@ -27,13 +24,13 @@ import static org.mockito.Mockito.when;
@SpringBootTest @SpringBootTest
public class ExerciseServiceTest { class ExerciseServiceTest {
@MockBean @MockBean
ExerciseRepository repository; ExerciseRepository exerciseRepository;
@Autowired @Autowired
ExerciseService service; ExerciseService exerciseService;
Exercise exercise; Exercise exercise;
ExerciseCreateDto exercise2; ExerciseCreateDto exercise2;
...@@ -41,35 +38,34 @@ public class ExerciseServiceTest { ...@@ -41,35 +38,34 @@ public class ExerciseServiceTest {
@BeforeEach @BeforeEach
void setup() { void setup() {
exercise = new Exercise("idioms", "exercise on basic idioms",2,1L,new HashSet<Question>()); exercise = new Exercise("idioms", "exercise on basic idioms", 2, 1L, new HashSet<>());
exercise2 = new ExerciseCreateDto("idioms1", "exercise on intermediate idioms", 2, 0); exercise2 = new ExerciseCreateDto("idioms1", "exercise on intermediate idioms", 2, 0);
exercise3 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L); exercise3 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L);
} }
@Test @Test
void create() { void create() {
when(repository.save(exercise)).thenReturn(exercise); when(exerciseRepository.save(exercise)).thenReturn(exercise);
Exercise result = service.create(exercise); Exercise result = exerciseService.create(exercise);
Assertions.assertEquals(exercise, result); Assertions.assertEquals(exercise, result);
verify(repository).save(exercise); verify(exerciseRepository).save(exercise);
} }
@Test @Test
void notFound() { void notFound() {
when(repository.findById(anyLong())).thenReturn(Optional.empty()); when(exerciseRepository.findById(anyLong())).thenReturn(Optional.empty());
Assertions.assertThrows(EntityNotFoundException.class, () -> service.find(anyLong())); Assertions.assertThrows(EntityNotFoundException.class, () -> exerciseService.find(anyLong()));
} }
@Test @Test
void find() { void find() {
when(repository.findById(anyLong())).thenReturn(Optional.of(exercise)); when(exerciseRepository.findById(anyLong())).thenReturn(Optional.of(exercise));
Exercise result = service.find(anyLong()); Exercise result = exerciseService.find(anyLong());
Assertions.assertEquals(exercise, result); Assertions.assertEquals(exercise, result);
verify(repository).findById(anyLong()); verify(exerciseRepository).findById(anyLong());
} }
@Test @Test
...@@ -77,24 +73,24 @@ public class ExerciseServiceTest { ...@@ -77,24 +73,24 @@ public class ExerciseServiceTest {
Pageable pageable = PageRequest.of(0, 10); Pageable pageable = PageRequest.of(0, 10);
Page<Exercise> page = new PageImpl<>(Collections.emptyList(), pageable, 0); Page<Exercise> page = new PageImpl<>(Collections.emptyList(), pageable, 0);
when(repository.findAll(pageable)).thenReturn(page); when(exerciseRepository.findAll(pageable)).thenReturn(page);
Page<Exercise> result = service.findAll(0); Page<Exercise> result = exerciseService.findAll(0);
Assertions.assertEquals(page, result); Assertions.assertEquals(page, result);
verify(repository).findAll(pageable); verify(exerciseRepository).findAll(pageable);
} }
@Test @Test
void findPerDiffPerCourse() { void findByCourseIdAndDifficulty() {
PageRequest pageable = PageRequest.of(0, 10); PageRequest pageable = PageRequest.of(0, 10);
Page<Exercise> page = new PageImpl<>(Collections.emptyList(), pageable, 0); Page<Exercise> page = new PageImpl<>(Collections.emptyList(), pageable, 0);
when(repository.filterPerDifficultyPerCourse(pageable,1L,2)).thenReturn(page); when(exerciseRepository.findByCourseIdAndDifficulty(1L, 2, pageable)).thenReturn(page);
Page<Exercise> result = service.findPerDifficultyPerCourse(0,1L,2); Page<Exercise> result = exerciseService.findByCourseIdAndDifficulty(1L, 2, 0);
Assertions.assertEquals(page, result); Assertions.assertEquals(page, result);
verify(repository).filterPerDifficultyPerCourse(pageable,1L,2); verify(exerciseRepository).findByCourseIdAndDifficulty(1L, 2, pageable);
} }
...@@ -103,13 +99,13 @@ public class ExerciseServiceTest { ...@@ -103,13 +99,13 @@ public class ExerciseServiceTest {
PageRequest pageable = PageRequest.of(0, 10); PageRequest pageable = PageRequest.of(0, 10);
Page<Question> page = new PageImpl<>(Collections.emptyList(), pageable, 0); Page<Question> page = new PageImpl<>(Collections.emptyList(), pageable, 0);
when(repository.getQuestions(pageable,1L)).thenReturn(page); when(exerciseRepository.getQuestions(pageable, 1L)).thenReturn(page);
when(repository.existsById(1L)).thenReturn(true); when(exerciseRepository.existsById(1L)).thenReturn(true);
Page<Question> result = service.getQuestions(1L,0); Page<Question> result = exerciseService.getQuestions(1L, 0);
Assertions.assertEquals(page, result); Assertions.assertEquals(page, result);
verify(repository).getQuestions(pageable,1L); verify(exerciseRepository).getQuestions(pageable, 1L);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment