Skip to content

Commit cef4a44

Browse files
committed
GP-66 upgrade tests
1 parent 73ca234 commit cef4a44

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

3-0-spring-mvc/3-0-1-hello-spring-mvc/src/main/resources/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<body>
1414
<div class="welcome">
1515
<h1>Welcome!</h1>
16-
<img src="https://avatars.githubusercontent.com/u/29048529?s=280&v=4" alt="Bobocode">
16+
<img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/bobo_logo.png" style="width: 50%"/>
1717
<a href="notes.html" th:href="@{/notes}">Click here to continue</a>
1818
</div>
1919
</body>
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11
import com.bobocode.mvc.HelloSpringMvcApp;
2-
import com.bobocode.mvc.model.Note;
2+
import com.bobocode.mvc.controller.NoteController;
33
import com.bobocode.mvc.data.Notes;
4+
import com.bobocode.mvc.model.Note;
45
import org.junit.jupiter.api.Test;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
79
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.mock.mockito.MockBean;
811
import org.springframework.test.web.servlet.MockMvc;
912
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
1013

11-
import static org.junit.jupiter.api.Assertions.assertEquals;
12-
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
import java.util.List;
15+
16+
import static org.mockito.Mockito.verify;
17+
import static org.mockito.Mockito.when;
1318
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1419
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
15-
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
1620
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
1721

18-
@SpringBootTest(classes = HelloSpringMvcApp.class)
1922
@AutoConfigureMockMvc
23+
@SpringBootTest(classes = HelloSpringMvcApp.class)
2024
public class NoteControllerTest {
21-
22-
@Autowired
25+
@MockBean
2326
private Notes notes;
2427

2528
@Autowired
2629
private MockMvc mockMvc;
2730

2831
@Test
2932
void getAllNotes() throws Exception {
30-
notes.add(new Note("Title 1", "Text 1"));
33+
List<Note> noteList = List.of(
34+
new Note("Test", "Hello, World!"),
35+
new Note("Greeting", "Hey")
36+
);
37+
when(notes.getAll()).thenReturn(noteList);
3138

3239
mockMvc.perform(get("/notes"))
3340
.andExpect(status().isOk())
3441
.andExpect(content().contentType("text/html;charset=UTF-8"))
3542
.andExpect(model().attributeExists("noteList"))
36-
.andExpect(model().attribute("noteList", notes.getAll()));
43+
.andExpect(model().attribute("noteList", noteList));
3744
}
3845

3946
@Test
4047
void addNote() throws Exception {
41-
Note note = new Note("Title 2", "Text 2");
42-
assertTrue(notes.getAll().isEmpty());
48+
Note note = new Note("Test", "Hello, World!");
4349

4450
mockMvc.perform(post("/notes")
4551
.param("title", note.getTitle())
@@ -48,9 +54,6 @@ void addNote() throws Exception {
4854
.andExpect(status().is3xxRedirection())
4955
.andExpect(MockMvcResultMatchers.redirectedUrl("/notes"));
5056

51-
int lastElementIndex = notes.getAll().size() - 1;
52-
53-
assertEquals("Title 2", notes.getAll().get(lastElementIndex).getTitle());
54-
assertEquals("Text 2", notes.getAll().get(lastElementIndex).getText());
57+
verify(notes).add(note);
5558
}
5659
}

3-0-spring-mvc/3-0-1-hello-spring-mvc/src/test/java/RestControllerTest.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
99
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.mock.mockito.MockBean;
1011
import org.springframework.http.MediaType;
1112
import org.springframework.test.web.servlet.MockMvc;
1213

13-
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import java.util.List;
15+
16+
import static org.mockito.Mockito.verify;
17+
import static org.mockito.Mockito.when;
1418
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1519
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
1620
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
@@ -20,39 +24,46 @@
2024
@SpringBootTest(classes = HelloSpringMvcApp.class)
2125
public class RestControllerTest {
2226

23-
@Autowired
27+
@MockBean
2428
private Notes notes;
2529

2630
@Autowired
2731
private MockMvc mockMvc;
2832

2933
@Test
3034
void getAllNotes() throws Exception {
31-
notes.add(new Note("Title 1", "Text 1"));
35+
List<Note> noteList = List.of(
36+
new Note("Test", "Hello, World!"),
37+
new Note("Greeting", "Hey")
38+
);
39+
when(notes.getAll()).thenReturn(noteList);
3240

3341
mockMvc.perform(get("/api/notes"))
3442
.andExpect(status().isOk())
35-
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
36-
37-
int lastElementIndex = notes.getAll().size() - 1;
38-
39-
assertEquals("Title 1", notes.getAll().get(lastElementIndex).getTitle());
40-
assertEquals("Text 1", notes.getAll().get(lastElementIndex).getText());
43+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
44+
.andExpect(content().json("[\n" +
45+
" {\n" +
46+
" \"title\": \"Test\",\n" +
47+
" \"text\": \"Hello, World!\"\n" +
48+
" },\n" +
49+
" {\n" +
50+
" \"title\": \"Greeting\",\n" +
51+
" \"text\": \"Hey\"\n" +
52+
" }\n" +
53+
"]"));
4154
}
4255

4356
@Test
4457
void addNote() throws Exception {
45-
Note note = new Note("Title 2", "Title 2");
58+
Note note = new Note("Test", "Hello, World!");
59+
4660
mockMvc.perform(post("/api/notes")
4761
.contentType(MediaType.APPLICATION_JSON)
4862
.content(asJsonString(note))
4963
)
5064
.andExpect(status().isOk());
5165

52-
int lastElementIndex = notes.getAll().size() - 1;
53-
54-
assertEquals("Title 2", notes.getAll().get(lastElementIndex).getTitle());
55-
assertEquals("Title 2", notes.getAll().get(lastElementIndex).getText());
66+
verify(notes).add(note);
5667
}
5768

5869
@Test

0 commit comments

Comments
 (0)