44import com .example .postgresdemo .repository .QuestionRepository ;
55
66import org .hamcrest .Matchers ;
7+ import org .junit .jupiter .api .AfterEach ;
8+ import org .junit .jupiter .api .BeforeEach ;
79import org .junit .jupiter .api .Test ;
810import org .springframework .beans .factory .annotation .Autowired ;
911import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
@@ -38,61 +40,52 @@ public class QuestionControllerTest {
3840 @ Autowired
3941 private QuestionController questionController ;
4042
41- @ Test
42- public void testCreateMockMvc () {
43- assertNotNull (mockMvc );
44- }
45-
46- private boolean fillQuestions (Integer number ) {
47- try {
48- for (int i = 0 ; i < number ; i ++) {
49- Question question = new Question ();
50- question .setTitle ("Question " + i );
51- question .setDescription ("Description " + i );
52- questionRepository .save (question );
53- }
54- return true ;
55- } catch (Exception e ) {
56- return false ;
43+ private void fillQuestions (Integer number ) {
44+ for (int i = 0 ; i < number ; i ++) {
45+ Question question = new Question ();
46+ question .setTitle ("Question " + i );
47+ question .setDescription ("Description " + i );
48+ questionRepository .save (question );
5749 }
5850 }
5951
60- private boolean deleteQuestions () {
61- try {
62- questionRepository .deleteAll ();
63- return true ;
64- } catch (Exception e ) {
65- return false ;
66- }
67- }
68-
69- private void deleteQuestionsWithExceptionOnFail () throws Exception {
70- if (!deleteQuestions ()) {
71- throw new Exception ("Failed to delete questions" );
72- }
52+ @ BeforeEach
53+ @ AfterEach
54+ public void deleteQuestions () {
55+ questionRepository .deleteAll ();
7356 }
7457
7558 @ Test
76- public void testGetQuestions () throws Exception {
77- deleteQuestionsWithExceptionOnFail ();
59+ public void testGetQuestionsWithAmountLessThanPageSize () throws Exception {
60+ int assertionNumber = 10 ;
61+ int pageSize = 20 ;
7862
79- for (int assertionNumber = 0 ; assertionNumber < 100 ; assertionNumber ++) {
80- int pageSize = 20 ;
63+ fillQuestions (assertionNumber );
8164
82- if (!fillQuestions (assertionNumber )) {
83- throw new Exception ("Failed to fill questions" );
84- }
65+ mockMvc .perform (get ("/questions" )
66+ .contentType (MediaType .APPLICATION_JSON ))
67+ .andExpect (status ().isOk ())
68+ .andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
69+ .andExpect (MockMvcResultMatchers .jsonPath ("$.content.length()" , Matchers .equalTo (assertionNumber )))
70+ .andExpect (MockMvcResultMatchers .jsonPath ("$.totalElements" , Matchers .equalTo (assertionNumber )))
71+ .andExpect (MockMvcResultMatchers .jsonPath ("$.totalPages" , Matchers .equalTo (1 )));
72+ }
8573
86- mockMvc .perform (get ("/questions" )
87- .contentType (MediaType .APPLICATION_JSON ))
88- .andExpect (status ().isOk ())
89- .andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
90- .andExpect (MockMvcResultMatchers .jsonPath ("$.content.length()" , Matchers .equalTo (Math .min (assertionNumber , pageSize ))))
91- .andExpect (MockMvcResultMatchers .jsonPath ("$.totalElements" , Matchers .equalTo (assertionNumber ))) // Assert total elements
92- .andExpect (MockMvcResultMatchers .jsonPath ("$.totalPages" , Matchers .equalTo ((int ) Math .ceil (assertionNumber / (double ) pageSize )))); // Assert total pages for 10 items per page
74+ @ Test
75+ public void testGetQuestionsWithAmountMoreThanPageSize () throws Exception {
76+ int assertionNumber = 30 ;
77+ int pageSize = 20 ;
78+ int totalPages = (int ) Math .ceil (assertionNumber / (double ) pageSize );
9379
94- deleteQuestionsWithExceptionOnFail ();
95- }
80+ fillQuestions (assertionNumber );
81+
82+ mockMvc .perform (get ("/questions" )
83+ .contentType (MediaType .APPLICATION_JSON ))
84+ .andExpect (status ().isOk ())
85+ .andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
86+ .andExpect (MockMvcResultMatchers .jsonPath ("$.content.length()" , Matchers .equalTo (pageSize )))
87+ .andExpect (MockMvcResultMatchers .jsonPath ("$.totalElements" , Matchers .equalTo (assertionNumber )))
88+ .andExpect (MockMvcResultMatchers .jsonPath ("$.totalPages" , Matchers .equalTo (totalPages )));
9689 }
9790
9891 @ Test
@@ -107,8 +100,6 @@ public void testCreateCorrectQuestion() throws Exception {
107100 .andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
108101 .andExpect (MockMvcResultMatchers .jsonPath ("$.title" , Matchers .equalTo ("Question 1" )))
109102 .andExpect (MockMvcResultMatchers .jsonPath ("$.description" , Matchers .equalTo ("Description 1" )));
110-
111- deleteQuestionsWithExceptionOnFail ();
112103 }
113104
114105 @ Test
@@ -119,8 +110,6 @@ public void testCreateQuestionWithoutTitle() throws Exception {
119110 " \" description\" : \" Description\" \n " +
120111 "}" ))
121112 .andExpect (status ().is4xxClientError ());
122-
123- deleteQuestionsWithExceptionOnFail ();
124113 }
125114
126115 @ Test
@@ -132,23 +121,20 @@ public void testCreateQuestionWithTitleLesThenThreeChars() throws Exception {
132121 " \" description\" : \" Description\" \n " +
133122 "}" ))
134123 .andExpect (status ().is4xxClientError ());
135-
136- deleteQuestionsWithExceptionOnFail ();
137124 }
138125
139126 @ Test
140127 public void testCreateQuestionWithTitleMoreThenHundredChars () throws Exception {
141128 int numberOfChars = 101 ;
142129 String title = CharBuffer .allocate (numberOfChars ).toString ().replace ('\0' , 'T' );
130+
143131 mockMvc .perform (MockMvcRequestBuilders .post ("/questions" )
144132 .contentType (MediaType .APPLICATION_JSON )
145133 .content ("{\n " +
146134 " \" title\" : \" " + title + "\" ,\n " +
147135 " \" description\" : \" Description\" \n " +
148136 "}" ))
149137 .andExpect (status ().is4xxClientError ());
150-
151- deleteQuestionsWithExceptionOnFail ();
152138 }
153139
154140 @ Test
@@ -162,13 +148,10 @@ public void testCreateQuestionWithoutDescription() throws Exception {
162148 .andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
163149 .andExpect (MockMvcResultMatchers .jsonPath ("$.title" , Matchers .equalTo ("Question 1" )))
164150 .andExpect (MockMvcResultMatchers .jsonPath ("$.description" , Matchers .equalTo (null )));
165-
166- deleteQuestionsWithExceptionOnFail ();
167151 }
168152
169153 @ Test
170154 public void testUpdateQuestion () throws Exception {
171- deleteQuestionsWithExceptionOnFail ();
172155 fillQuestions (1 );
173156 long questionId = questionRepository .findAll ().get (0 ).getId ();
174157
@@ -182,13 +165,14 @@ public void testUpdateQuestion() throws Exception {
182165 .andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
183166 .andExpect (MockMvcResultMatchers .jsonPath ("$.title" , Matchers .equalTo ("Edited Question 1" )))
184167 .andExpect (MockMvcResultMatchers .jsonPath ("$.description" , Matchers .equalTo ("Edited Description 1" )));
185-
186- deleteQuestionsWithExceptionOnFail ();
187168 }
188169
189170 @ Test
190171 public void testUpdateQuestionWithNonExistingId () throws Exception {
191- mockMvc .perform (MockMvcRequestBuilders .put ("/questions/1" )
172+ fillQuestions (1 );
173+ long questionId = questionRepository .findAll ().get (0 ).getId ();
174+
175+ mockMvc .perform (MockMvcRequestBuilders .put ("/questions/" + (questionId + 1 ))
192176 .contentType (MediaType .APPLICATION_JSON )
193177 .content ("{\n " +
194178 " \" title\" : \" Edited Question 1\" ,\n " +
@@ -199,14 +183,11 @@ public void testUpdateQuestionWithNonExistingId() throws Exception {
199183
200184 @ Test
201185 public void testDeleteQuestion () throws Exception {
202- deleteQuestionsWithExceptionOnFail ();
203186 fillQuestions (1 );
204187 long questionId = questionRepository .findAll ().get (0 ).getId ();
205188
206189 mockMvc .perform (MockMvcRequestBuilders .delete ("/questions/" + questionId )
207190 .contentType (MediaType .APPLICATION_JSON ))
208191 .andExpect (status ().isOk ());
209-
210- deleteQuestionsWithExceptionOnFail ();
211192 }
212193}
0 commit comments