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;
import jakarta.persistence.EntityNotFoundException;
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.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
......@@ -27,13 +24,13 @@ import static org.mockito.Mockito.when;
@SpringBootTest
public class ExerciseServiceTest {
class ExerciseServiceTest {
@MockBean
ExerciseRepository repository;
ExerciseRepository exerciseRepository;
@Autowired
ExerciseService service;
ExerciseService exerciseService;
Exercise exercise;
ExerciseCreateDto exercise2;
......@@ -41,35 +38,34 @@ public class ExerciseServiceTest {
@BeforeEach
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);
exercise3 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L);
}
@Test
void create() {
when(repository.save(exercise)).thenReturn(exercise);
Exercise result = service.create(exercise);
when(exerciseRepository.save(exercise)).thenReturn(exercise);
Exercise result = exerciseService.create(exercise);
Assertions.assertEquals(exercise, result);
verify(repository).save(exercise);
verify(exerciseRepository).save(exercise);
}
@Test
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
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);
verify(repository).findById(anyLong());
verify(exerciseRepository).findById(anyLong());
}
@Test
......@@ -77,24 +73,24 @@ public class ExerciseServiceTest {
Pageable pageable = PageRequest.of(0, 10);
Page<Exercise> page = new PageImpl<>(Collections.emptyList(), pageable, 0);
when(repository.findAll(pageable)).thenReturn(page);
Page<Exercise> result = service.findAll(0);
when(exerciseRepository.findAll(pageable)).thenReturn(page);
Page<Exercise> result = exerciseService.findAll(0);
Assertions.assertEquals(page, result);
verify(repository).findAll(pageable);
verify(exerciseRepository).findAll(pageable);
}
@Test
void findPerDiffPerCourse() {
void findByCourseIdAndDifficulty() {
PageRequest pageable = PageRequest.of(0, 10);
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);
verify(repository).filterPerDifficultyPerCourse(pageable,1L,2);
verify(exerciseRepository).findByCourseIdAndDifficulty(1L, 2, pageable);
}
......@@ -103,13 +99,13 @@ public class ExerciseServiceTest {
PageRequest pageable = PageRequest.of(0, 10);
Page<Question> page = new PageImpl<>(Collections.emptyList(), pageable, 0);
when(repository.getQuestions(pageable,1L)).thenReturn(page);
when(repository.existsById(1L)).thenReturn(true);
when(exerciseRepository.getQuestions(pageable, 1L)).thenReturn(page);
when(exerciseRepository.existsById(1L)).thenReturn(true);
Page<Question> result = service.getQuestions(1L,0);
Page<Question> result = exerciseService.getQuestions(1L, 0);
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