Skip to content

Commit

Permalink
Added tests for question class
Browse files Browse the repository at this point in the history
  • Loading branch information
sinne10 committed Mar 8, 2024
1 parent 7113824 commit 1f1273a
Showing 1 changed file with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.wiq.wiq.services.questionGenerator.question;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class QuestionTests {

private Question question;

@BeforeEach
void setUp() {
List<String> answers = Arrays.asList("A", "B", "C");
question = new Question("What is the capital of France?", answers);
}

@Test
void testGetQuestion() {
assertEquals("What is the capital of France?", question.getQuestion());
}

@Test
void testSetQuestion() {
question.setQuestion("What is the capital of Germany?");
assertEquals("What is the capital of Germany?", question.getQuestion());
}

@Test
void testGetAnswers() {
List<String> expectedAnswers = Arrays.asList("A", "B", "C");
assertEquals(expectedAnswers, question.getAnswers());
}

@Test
void testSetAnswers() {
List<String> newAnswers = Arrays.asList("X", "Y", "Z");
question.setAnswers(newAnswers);
assertEquals(newAnswers, question.getAnswers());
}

@Test
void testAddRightAnswer() {
question.addRightAnswer("Paris");
List<String> expectedAnswers = Arrays.asList("Paris", "A", "B", "C");
assertEquals(expectedAnswers, question.getAnswers());
}

@Test
void testGetJSON() {
try {
JSONObject expectedJson = new JSONObject()
.put("question", "What is the capital of France?")
.put("answers", new JSONArray().put("A").put("B").put("C"));
assertEquals(expectedJson.toString(), question.getJSON().toString());
} catch (JSONException e) {
fail("JSONException occurred: " + e.getMessage());
}
}
}

0 comments on commit 1f1273a

Please sign in to comment.