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

Fix AnswerFacadeTest

parent 8d41e821
No related branches found
No related tags found
3 merge requests!31M2,!30M2 exercise,!29M2 exercise
...@@ -2,90 +2,52 @@ package org.fuseri.moduleexercise.answer; ...@@ -2,90 +2,52 @@ package org.fuseri.moduleexercise.answer;
import org.fuseri.model.dto.exercise.AnswerCreateDto; import org.fuseri.model.dto.exercise.AnswerCreateDto;
import org.fuseri.model.dto.exercise.AnswerDto; import org.fuseri.model.dto.exercise.AnswerDto;
import org.fuseri.model.dto.exercise.AnswerInQuestionCreateDto;
import org.fuseri.model.dto.exercise.AnswersCreateDto;
import org.fuseri.model.dto.exercise.QuestionUpdateDto;
import org.fuseri.moduleexercise.exercise.Exercise; import org.fuseri.moduleexercise.exercise.Exercise;
import org.fuseri.moduleexercise.question.Question; import org.fuseri.moduleexercise.question.Question;
import org.fuseri.moduleexercise.question.QuestionService;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@SpringBootTest @SpringBootTest
public class AnswerFacadeTest { class AnswerFacadeTest {
private final Exercise exercise = new Exercise();
private final Question question = new Question("text",new HashSet<>(),exercise);
private final AnswerDto answerDto = new AnswerDto("name",true);
private final AnswerCreateDto answerCreateDto = new AnswerCreateDto("name",true,1L);
private final AnswerInQuestionCreateDto inQuestionCreateDto = new AnswerInQuestionCreateDto("name",true);
private final AnswersCreateDto answersCreateDto = new AnswersCreateDto(1L, List.of(inQuestionCreateDto));
private final QuestionUpdateDto questionUpdateDto = QuestionUpdateDto.builder().build();
private final Answer answer = new Answer("name",true,question);
@MockBean
AnswerRepository repository;
@Autowired @Autowired
AnswerFacade facade; private AnswerFacade answerFacade;
@MockBean @MockBean
AnswerService service; private AnswerService answerService;
@MockBean @MockBean
QuestionService questionService; private AnswerMapper answerMapper;
@MockBean private final AnswerDto answerDto = new AnswerDto("name", true);
AnswerMapper mapper; private final AnswerCreateDto answerCreateDto = new AnswerCreateDto("name", true, 1L);
@Test private final Question question = new Question("text", new HashSet<>(), new Exercise());
void create() { private final Answer answer = new Answer("name", true, question);
long id = 1;
when(service.find(id)).thenReturn(answer);
when(mapper.fromCreateDto(inQuestionCreateDto)).thenReturn(answer);
when(service.create(answer)).thenReturn(answer);
when(mapper.toDto(answer)).thenReturn(answerDto);
when(questionService.find(id)).thenReturn(question);
when(mapper.toDtoList(List.of(answer))).thenReturn(List.of(answerDto));
var actualDto = facade.createMultiple(answersCreateDto);
assertEquals(List.of(answerDto), actualDto);
}
@Test @Test
void update() { void update() {
Long id = 1L; long id = 1L;
when(repository.existsById(id)).thenReturn(true); when(answerMapper.fromCreateDto(answerCreateDto)).thenReturn(answer);
when(service.find(id)).thenReturn(answer); when(answerService.update(id, answer)).thenReturn(answer);
when(mapper.fromCreateDto(answerCreateDto)).thenReturn(answer); when(answerMapper.toDto(answer)).thenReturn(answerDto);
when(service.update(answer)).thenReturn(answer);
when(mapper.toDto(answer)).thenReturn(answerDto);
AnswerDto actualDto = facade.update(id,answerCreateDto);
AnswerDto actualDto = answerFacade.update(id, answerCreateDto);
assertEquals(answerDto, actualDto); assertEquals(answerDto, actualDto);
} }
@Test @Test
void testDelete() { void delete() {
Long id = 1L; long id = 1L;
when(service.find(id)).thenReturn(answer); answerFacade.delete(id);
when(questionService.find(answer.getId())).thenReturn(question); verify(answerService).delete(id);
facade.delete(id);
verify(service).delete(id);
} }
} }
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