dianamiraflor / 4806-project

0 stars 1 forks source link

Add integration tests for endpoints #11

Open dianamiraflor opened 1 year ago

dianamiraflor commented 1 year ago
dianamiraflor commented 1 year ago

image

dianamiraflor commented 1 year ago

My goodness all I had to do was remove @Autowired repos in SurveyController .................................................................................

dianamiraflor commented 1 year ago

/**

package com.app.surveymonkey;

import com.app.surveymonkey.controller.SurveyController; import com.app.surveymonkey.questions.Question; import com.app.surveymonkey.repositories.SurveyRepo; import com.app.surveymonkey.survey.Survey; import com.app.surveymonkey.surveyor.Surveyor; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc;

import java.util.ArrayList; import java.util.List;

import static org.mockito.Mockito.when; 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.result.MockMvcResultMatchers.*;

@WebMvcTest(SurveyController.class) public class SurveyControllerDBSetupTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private SurveyRepo surveyRepo;

@BeforeEach
public void setUp() {
    Survey survey = new Survey();
    survey.setId(1);
    survey.setSurveyName("SurveyTest");

    Question mcq1 = new Question();
    mcq1.setText("MCQ_Test");
    mcq1.setType(Question.QuestionType.CHOICE);
    List<String> choices_list = new ArrayList<>();
    choices_list.add("Choice1");
    choices_list.add("Choice2");
    choices_list.add("Choice3");
    mcq1.setChoices(choices_list);
    mcq1.setSurvey(survey);

    Question rq1 = new Question();
    rq1.setText("RQ_Test");
    rq1.setType(Question.QuestionType.RANGE);
    rq1.setRangeMin(1);
    rq1.setRangeMax(3);
    rq1.setSurvey(survey);

    Question tq1 = new Question();
    tq1.setText("TQ_Test");
    tq1.setType(Question.QuestionType.TEXT);
    tq1.setSurvey(survey);

    survey.addQuestion(mcq1);
    survey.addQuestion(rq1);
    survey.addQuestion(tq1);

    survey.setOpen(true);

    surveyRepo.save(survey);
}

@Test
void testHomePage() throws Exception {
    this.mockMvc
            .perform(get("/home"))
            .andExpect(status().isOk())
            .andExpect(view().name("HomePage"));

}

@Test
void shouldGetSurvey() throws Exception {
    this.mockMvc
            .perform(get("/viewsurvey/1"))
            .andExpect(status().isOk())
            .andExpect(view().name("survey-view"))
            .andExpect(model().attributeExists("survey"));
}

@Test
void shouldGetAvailableSurveys() throws Exception {
    this.mockMvc
            .perform(get("/availableSurveys"))
            .andExpect(status().isOk())
            .andExpect(view().name("view-surveys"))
            .andExpect(model().attributeExists("surveys"));

}

}

Does this current test setup not work with MockMVC?

dianamiraflor commented 1 year ago

So I tried adding integration tests -> but so far as I know, the one I created will only work if the CommandLineRunner isn't there. It will also work once we have the create survey stuff done because then we can remove CommandLineRunner.

NOTE: Applied to MockMVC