Skip to content

Commit 75e197b

Browse files
author
Ivan
committed
Fix controller tests
1 parent d304fab commit 75e197b

File tree

3 files changed

+56
-40
lines changed

3 files changed

+56
-40
lines changed
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import com.bobocode.mvc.HelloSpringMvcApp;
22
import com.bobocode.mvc.model.Note;
33
import com.bobocode.mvc.storage.Notes;
4+
import org.junit.jupiter.api.BeforeEach;
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;
78
import org.springframework.boot.test.context.SpringBootTest;
8-
import org.springframework.http.MediaType;
99
import org.springframework.test.web.servlet.MockMvc;
1010
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
1111

1212
import static org.junit.jupiter.api.Assertions.assertEquals;
1313
import static org.junit.jupiter.api.Assertions.assertTrue;
1414
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1515
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
1617
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
1718

1819
@SpringBootTest(classes = HelloSpringMvcApp.class)
@@ -27,40 +28,30 @@ public class NoteControllerTest {
2728

2829
@Test
2930
void getAllNotes() throws Exception {
30-
fillNotes(
31-
new Note("title1", "text1"),
32-
new Note("title2", "text2")
33-
);
31+
notes.add(new Note("Title 1", "Text 1"));
3432

3533
mockMvc.perform(get("/notes"))
3634
.andExpect(status().isOk())
3735
.andExpect(content().contentType("text/html;charset=UTF-8"))
38-
.andExpect(model().size(2))
3936
.andExpect(model().attributeExists("notes"))
4037
.andExpect(model().attribute("notes", notes.getAll()));
4138
}
4239

4340
@Test
4441
void addNote() throws Exception {
45-
Note note = new Note("note", "text");
42+
Note note = new Note("Title 2", "Text 2");
4643
assertTrue(notes.getAll().isEmpty());
4744

4845
mockMvc.perform(post("/notes")
49-
.contentType(MediaType.MULTIPART_FORM_DATA)
5046
.param("title", note.getTitle())
5147
.param("text", note.getText())
5248
)
5349
.andExpect(status().is3xxRedirection())
5450
.andExpect(MockMvcResultMatchers.redirectedUrl("/notes"));
5551

56-
assertEquals(1, notes.getAll().size());
57-
assertEquals("note", notes.getAll().get(0).getTitle());
58-
assertEquals("text", notes.getAll().get(0).getText());
59-
}
52+
int lastElementIndex = notes.getAll().size() - 1;
6053

61-
private void fillNotes(Note... notes) {
62-
for (Note note : notes) {
63-
this.notes.add(note);
64-
}
54+
assertEquals("Title 2", notes.getAll().get(lastElementIndex).getTitle());
55+
assertEquals("Text 2", notes.getAll().get(lastElementIndex).getText());
6556
}
6657
}

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import com.bobocode.mvc.storage.Notes;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import lombok.SneakyThrows;
6-
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Test;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1011
import org.springframework.boot.test.context.SpringBootTest;
1112
import org.springframework.http.MediaType;
1213
import org.springframework.test.web.servlet.MockMvc;
13-
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
1414

1515
import static org.hamcrest.Matchers.is;
1616
import static org.junit.jupiter.api.Assertions.*;
@@ -30,30 +30,31 @@ public class RestControllerTest {
3030

3131
@Test
3232
void getAll() throws Exception {
33-
fillNotes(new Note("title 1", "text 1"), new Note("title 2", "text 2"));
33+
notes.add(new Note("Title 1", "Text 1"));
3434

3535
mockMvc.perform(get("/api/notes"))
3636
.andExpect(status().isOk())
37-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
38-
.andExpect(jsonPath("$[0].title", is("title 1")))
39-
.andExpect(jsonPath("$[0].text", is("text 1")))
40-
.andExpect(jsonPath("$[1].title", is("title 2")))
41-
.andExpect(jsonPath("$[1].text", is("text 2")));
37+
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
38+
39+
int lastElementIndex = notes.getAll().size() - 1;
40+
41+
assertEquals("Title 1", notes.getAll().get(lastElementIndex).getTitle());
42+
assertEquals("Text 1", notes.getAll().get(lastElementIndex).getText());
4243
}
4344

4445
@Test
4546
void addNote() throws Exception {
46-
Note note = new Note("Note", "Note text");
47-
48-
assertTrue(notes.getAll().isEmpty());
47+
Note note = new Note("Title 2", "Title 2");
4948
mockMvc.perform(post("/api/notes")
5049
.contentType(MediaType.APPLICATION_JSON)
5150
.content(asJsonString(note))
5251
)
5352
.andExpect(status().isOk());
5453

55-
assertEquals(note.getTitle(), notes.getAll().get(0).getTitle());
56-
assertEquals(note.getText(), notes.getAll().get(0).getText());
54+
int lastElementIndex = notes.getAll().size() - 1;
55+
56+
assertEquals("Title 2", notes.getAll().get(lastElementIndex).getTitle());
57+
assertEquals("Title 2", notes.getAll().get(lastElementIndex).getText());
5758
}
5859

5960
@Test
@@ -69,12 +70,5 @@ void statusIs4xxWhenRequiredFieldsAreEmpty() throws Exception {
6970
private String asJsonString(Object object) {
7071
return new ObjectMapper().writeValueAsString(object);
7172
}
72-
73-
private void fillNotes(Note... notes) {
74-
for (Note note : notes) {
75-
this.notes.add(note);
76-
}
77-
}
78-
7973
}
8074

3-0-spring-mvc/pom.xml

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,49 @@
77
<groupId>com.bobocode</groupId>
88
<version>1.0-SNAPSHOT</version>
99
</parent>
10+
1011
<modelVersion>4.0.0</modelVersion>
1112

1213
<artifactId>3-0-spring-mvc</artifactId>
1314
<packaging>pom</packaging>
1415

15-
<modules>
16-
<module>3-0-1-hello-spring-mvc</module>
17-
</modules>
18-
1916
<properties>
2017
<maven.compiler.source>11</maven.compiler.source>
2118
<maven.compiler.target>11</maven.compiler.target>
2219
</properties>
2320

21+
<build>
22+
<plugins>
23+
<!-- <plugin>-->
24+
<!-- <groupId>org.apache.maven.plugins</groupId>-->
25+
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
26+
<!-- <configuration>-->
27+
<!-- <source>11</source>-->
28+
<!-- <target>11</target>-->
29+
<!-- </configuration>-->
30+
<!-- </plugin>-->
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-surefire-plugin</artifactId>
34+
<version>2.21.0</version>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.junit.platform</groupId>
38+
<artifactId>junit-platform-surefire-provider</artifactId>
39+
<version>1.2.0-M1</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter-engine</artifactId>
44+
<version>5.2.0-M1</version>
45+
</dependency>
46+
</dependencies>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
51+
<modules>
52+
<module>3-0-1-hello-spring-mvc</module>
53+
</modules>
54+
2455
</project>

0 commit comments

Comments
 (0)