|
2 | 2 |
|
3 | 3 | import com.example.postgresdemo.model.Question; |
4 | 4 | import com.example.postgresdemo.repository.QuestionRepository; |
| 5 | + |
| 6 | +import org.hamcrest.Matchers; |
5 | 7 | import org.junit.jupiter.api.Test; |
| 8 | +import org.mockito.Mockito; |
6 | 9 | import org.springframework.beans.factory.annotation.Autowired; |
7 | | -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 10 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
8 | 11 | import org.springframework.boot.test.context.SpringBootTest; |
9 | 12 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 13 | +import org.springframework.data.domain.Page; |
| 14 | +import org.springframework.data.domain.PageImpl; |
| 15 | +import org.springframework.data.domain.PageRequest; |
| 16 | +import org.springframework.data.domain.Pageable; |
| 17 | +import org.springframework.http.MediaType; |
10 | 18 | import org.springframework.test.web.servlet.MockMvc; |
| 19 | +import org.springframework.test.web.servlet.ResultMatcher; |
11 | 20 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
12 | 21 | import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
13 | 22 | import org.springframework.web.context.WebApplicationContext; |
14 | 23 |
|
| 24 | + |
| 25 | +import java.util.Arrays; |
15 | 26 | import java.util.List; |
16 | 27 |
|
17 | | -import static org.junit.Assert.assertNotNull; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
18 | 29 | import static org.mockito.Mockito.when; |
| 30 | +import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath; |
| 31 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 32 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 33 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 34 | + |
| 35 | +@SpringBootTest |
19 | 36 |
|
20 | | -//@SpringBootTest |
21 | | -//@WebMvcTest |
22 | | -@WebMvcTest(QuestionController.class) |
| 37 | +@AutoConfigureMockMvc |
23 | 38 | public class QuestionControllerTest { |
24 | | -// private QuestionRepository questionRepository; |
| 39 | + @Autowired |
| 40 | + private QuestionRepository questionRepository; |
25 | 41 |
|
26 | 42 | @Autowired |
27 | 43 | private WebApplicationContext webApplicationContext; |
28 | 44 |
|
29 | 45 | @Autowired |
30 | 46 | private MockMvc mockMvc; |
| 47 | + @Autowired |
| 48 | + private QuestionController questionController; |
31 | 49 |
|
32 | 50 | @Test |
33 | 51 | public void testCreateMockMvc() { |
34 | 52 | assertNotNull(mockMvc); |
35 | 53 | } |
36 | 54 |
|
| 55 | + private boolean fillQuestions(Integer number) { |
| 56 | + try { |
| 57 | + for (int i = 0; i < number; i++) { |
| 58 | + Question question = new Question(); |
| 59 | + question.setTitle("Question " + i); |
| 60 | + question.setDescription("Description " + i); |
| 61 | + questionRepository.save(question); |
| 62 | + } |
| 63 | + return true; |
| 64 | + } catch (Exception e) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private boolean deleteQuestions() { |
| 70 | + try { |
| 71 | + questionRepository.deleteAll(); |
| 72 | + return true; |
| 73 | + } catch (Exception e) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
37 | 78 | @Test |
38 | 79 | public void testGetQuestions() throws Exception { |
39 | | -// when(questionRepository.findAll()).thenReturn(List.of(new Question())); |
| 80 | + for (int assertionNumber = 0; assertionNumber < 100; assertionNumber++) { |
| 81 | + int pageSize = 20; |
40 | 82 |
|
41 | | - this.mockMvc |
42 | | - .perform(MockMvcRequestBuilders.get("/questions")) |
43 | | - .andExpect(MockMvcResultMatchers.status().isOk()); |
| 83 | + if (!fillQuestions(assertionNumber)) { |
| 84 | + throw new Exception("Failed to fill questions"); |
| 85 | + } |
| 86 | + |
| 87 | + mockMvc.perform(get("/questions") |
| 88 | + .contentType(MediaType.APPLICATION_JSON)) |
| 89 | + .andExpect(status().isOk()) |
| 90 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 91 | + .andExpect(MockMvcResultMatchers.jsonPath("$.content.length()", Matchers.equalTo(Math.min(assertionNumber, pageSize)))) |
| 92 | + .andExpect(MockMvcResultMatchers.jsonPath("$.totalElements", Matchers.equalTo(assertionNumber))) // Assert total elements |
| 93 | + .andExpect(MockMvcResultMatchers.jsonPath("$.totalPages", Matchers.equalTo((int) Math.ceil(assertionNumber / (double) pageSize)))); // Assert total pages for 10 items per page |
| 94 | + |
| 95 | + if (!deleteQuestions()) { |
| 96 | + throw new Exception("Failed to delete questions"); |
| 97 | + } |
| 98 | + } |
44 | 99 | } |
| 100 | + |
45 | 101 | } |
0 commit comments