Skip to content
Snippets Groups Projects

Exercise tests

Merged Martin Gargalovič requested to merge exercise-tests into exercise
3 files
+ 41
97
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -15,9 +15,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
@@ -41,14 +39,12 @@ public class AnswerTest {
private QuestionDto createQuestion(String id) throws Exception {
var question = new QuestionCreateDto("this statement is false", id,
List.of(new AnswerInQuestionCreateDto("dis a logical paradox",true)));
List.of(new AnswerInQuestionCreateDto("dis a logical paradox", true)));
var posted = mockMvc.perform(post("/questions")
.content(asJsonString(question))
.contentType(MediaType.APPLICATION_JSON));
// .andExpect(status().isOk());
var cont = posted.andReturn().getResponse().getContentAsString();
var res = objectMapper.readValue(cont, QuestionDto.class);
@@ -77,34 +73,27 @@ public class AnswerTest {
}
@Test
void testCreateAnswer() throws Exception {
List<AnswerDto> res = createAnswr();
var expected1 = new AnswerDto("True",false);
var expected2 = new AnswerDto("False",false);
var expected1 = new AnswerDto("True", false);
var expected2 = new AnswerDto("False", false);
assert(res.get(0).equals(expected1));
assert(res.get(1).equals(expected2));
assert (res.get(0).equals(expected1));
assert (res.get(1).equals(expected2));
}
}
private List<AnswerDto> createAnswr() throws Exception {
var exerciseId = createExercise();
var question = createQuestion(exerciseId);
var incorrect1 = new AnswerInQuestionCreateDto("True",false);
var incorrect2 = new AnswerInQuestionCreateDto("False",false);
var createAnswer = new AnswersCreateDto(question.getId(), List.of(incorrect1,incorrect2));
var content = asJsonString(createAnswer);
var incorrect1 = new AnswerInQuestionCreateDto("True", false);
var incorrect2 = new AnswerInQuestionCreateDto("False", false);
var createAnswer = new AnswersCreateDto(question.getId(), List.of(incorrect1, incorrect2));
var posted = mockMvc.perform(post("/answers")
.content(asJsonString(createAnswer))
@@ -123,7 +112,7 @@ public class AnswerTest {
var exerciseId = createExercise();
var question = createQuestion(exerciseId);
var gets = mockMvc.perform(get(String.format("/answers/%s",question.getId())));
var gets = mockMvc.perform(get(String.format("/answers/%s", question.getId())));
var content2 = gets.andReturn().getResponse().getContentAsString();
@@ -140,11 +129,11 @@ public class AnswerTest {
var exerciseId = createExercise();
var question = createQuestion(exerciseId);
var incorrect1 = new AnswerInQuestionCreateDto("True",false);
var incorrect2 = new AnswerInQuestionCreateDto("False",false);
var incorrect1 = new AnswerInQuestionCreateDto("True", false);
var incorrect2 = new AnswerInQuestionCreateDto("False", false);
var createAnswer = new AnswersCreateDto(question.getId(), List.of(incorrect1,incorrect2));
var createAnswer = new AnswersCreateDto(question.getId(), List.of(incorrect1, incorrect2));
var posted = mockMvc.perform(post("/answers")
@@ -166,16 +155,16 @@ public class AnswerTest {
""";
updated = String.format(updated,question.getId());
updated = String.format(updated, question.getId());
var puts = mockMvc.perform(put(String.format("/answers/%s",res.get(0).getId()))
var puts = mockMvc.perform(put(String.format("/answers/%s", res.get(0).getId()))
.content(updated).contentType(MediaType.APPLICATION_JSON));
var content = puts.andReturn().getResponse().getContentAsString();
var res2 = objectMapper.readValue(content,AnswerDto.class);
var res2 = objectMapper.readValue(content, AnswerDto.class);
var expected = new AnswerDto("dis true",false);
var expected = new AnswerDto("dis true", false);
assert res2.equals(expected);
Loading