@Test public void createQuestionnaireError() throws Exception { when(questionnaireRepositoryMock.save(any(Questionnaire.class))).thenReturn(null); mockMvc .perform( post("/questionnaires/") .header("Accept", "application/json") .contentType(MediaType.APPLICATION_JSON) .content("{ \"title\": \"title\", \"description\": \"aa\" }")) .andExpect(status().isInternalServerError()); }
@Test public void updateQuestionnaireError() throws Exception { when(questionnaireRepositoryMock.save(any(Questionnaire.class))).thenReturn(null); mockMvc .perform( put("/questionnaires/{id}", 1L) .header("Accept", "application/json") .contentType(MediaType.APPLICATION_JSON) .content("{ \"title\": \"title\", \"description\": \"aaa\" }")) .andExpect(status().isInternalServerError()); verify(questionnaireRepositoryMock, times(1)) .save(eq(new QuestionnaireBuilder(1L).title("title").description("aaa").build())); }
@Test public void findById_QuestionnaireFound_ShouldReturnFound() throws Exception { Questionnaire questionnaire = new QuestionnaireBuilder(1L).description("MyDescription").title("MyTitle").build(); when(questionnaireRepositoryMock.findOne(1L)).thenReturn(questionnaire); mockMvc .perform(get("/questionnaires/{id}", 1L).header("Accept", "application/json")) // .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.id", is(1))) .andExpect(jsonPath("$.title", is("MyTitle"))) .andExpect(jsonPath("$.description", is("MyDescription"))); }
@Test public void readAllQuestionnaires() throws Exception { Questionnaire q1 = new QuestionnaireBuilder(1L).title("title").description("aaa").build(); Questionnaire q2 = new QuestionnaireBuilder(2L).title("title2").description("aaa2").build(); when(questionnaireRepositoryMock.findAll()).thenReturn(Arrays.asList(q1, q2)); mockMvc .perform(get("/questionnaires/").header("Accept", "application/json")) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].id", is(1))) .andExpect(jsonPath("$[0].title", is("title"))) .andExpect(jsonPath("$[0].description", is("aaa"))) .andExpect(jsonPath("$[1].id", is(2))) .andExpect(jsonPath("$[1].title", is("title2"))) .andExpect(jsonPath("$[1].description", is("aaa2"))); verify(questionnaireRepositoryMock, times(1)).findAll(); }
@Test public void createQuestionnaire() throws Exception { Questionnaire q = new QuestionnaireBuilder(2L).title("title").description("aaa").build(); when(questionnaireRepositoryMock.save(any(Questionnaire.class))).thenReturn(q); mockMvc .perform( post("/questionnaires/") .header("Accept", "application/json") .contentType(MediaType.APPLICATION_JSON) // .contentType("application/json") .content("{ \"title\": \"title\", \"description\": \"aaa\" }")) .andExpect(status().isCreated()) .andExpect(jsonPath("$.id", is(2))) .andExpect(jsonPath("$.title", is("title"))) .andExpect(jsonPath("$.description", is("aaa"))); verify(questionnaireRepositoryMock, times(1)) .save(eq(new QuestionnaireBuilder(0L).title("title").description("aaa").build())); }