Skip to content

Commit 37174b5

Browse files
author
zahar.kalosha
committed
Add first correct test
1 parent 5b82e53 commit 37174b5

File tree

1 file changed

+66
-10
lines changed

1 file changed

+66
-10
lines changed

src/test/java/com/example/postgresdemo/controller/QuestionControllerTest.java

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,100 @@
22

33
import com.example.postgresdemo.model.Question;
44
import com.example.postgresdemo.repository.QuestionRepository;
5+
6+
import org.hamcrest.Matchers;
57
import org.junit.jupiter.api.Test;
8+
import org.mockito.Mockito;
69
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;
811
import org.springframework.boot.test.context.SpringBootTest;
912
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;
1018
import org.springframework.test.web.servlet.MockMvc;
19+
import org.springframework.test.web.servlet.ResultMatcher;
1120
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
1221
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
1322
import org.springframework.web.context.WebApplicationContext;
1423

24+
25+
import java.util.Arrays;
1526
import java.util.List;
1627

17-
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1829
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
1936

20-
//@SpringBootTest
21-
//@WebMvcTest
22-
@WebMvcTest(QuestionController.class)
37+
@AutoConfigureMockMvc
2338
public class QuestionControllerTest {
24-
// private QuestionRepository questionRepository;
39+
@Autowired
40+
private QuestionRepository questionRepository;
2541

2642
@Autowired
2743
private WebApplicationContext webApplicationContext;
2844

2945
@Autowired
3046
private MockMvc mockMvc;
47+
@Autowired
48+
private QuestionController questionController;
3149

3250
@Test
3351
public void testCreateMockMvc() {
3452
assertNotNull(mockMvc);
3553
}
3654

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+
3778
@Test
3879
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;
4082

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+
}
4499
}
100+
45101
}

0 commit comments

Comments
 (0)