24
24
import java .util .function .Supplier ;
25
25
26
26
import static org .junit .jupiter .api .Assertions .assertEquals ;
27
- import static org .mockito .BDDMockito .given ;
28
- import static org .mockito .BDDMockito . willDoNothing ;
27
+ import static org .mockito .BDDMockito .* ;
28
+ import static org .mockito .Mockito . verify ;
29
29
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
30
30
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
31
31
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
@@ -52,13 +52,16 @@ void getDocumentTest() throws Exception {
52
52
53
53
Document document = testDocumentFactory .get ();
54
54
55
- given (documentsService .getDocument (document .getName ())).willReturn (document );
55
+ String name = document .getName ();
56
+ given (documentsService .getDocument (name )).willReturn (document );
56
57
57
- mockMvc .perform (get ("/docs/{name}" , document . getName () )).andExpect (status ().isOk ())
58
- .andExpect (jsonPath ("$.name" ).value (document . getName () ))
58
+ mockMvc .perform (get ("/docs/{name}" , name )).andExpect (status ().isOk ())
59
+ .andExpect (jsonPath ("$.name" ).value (name ))
59
60
.andExpect (jsonPath ("$.content" ).value (document .getContent ()))
60
61
.andExpect (jsonPath ("$.id" ).value (document .getId ())).andReturn ();
61
62
63
+ then (documentsService ).should ().getDocument (name );
64
+
62
65
}
63
66
64
67
@ Test
@@ -75,26 +78,29 @@ void getAllDocumentsTest() throws Exception {
75
78
});
76
79
77
80
assertEquals (documents , documentsFromResponse );
81
+ then (documentsService ).should ().getAllDocuments ();
78
82
}
79
83
80
84
@ Test
81
85
void deleteDocumentTest () throws Exception {
82
86
83
87
Document document = testDocumentFactory .get ();
84
88
85
- willDoNothing ().given (documentsService ).removeDocument (document .getName ());
86
- given (documentsService .getDocument (document .getName ())).willReturn (document );
89
+ String name = document .getName ();
90
+ willDoNothing ().given (documentsService ).removeDocument (name );
91
+ given (documentsService .getDocument (name )).willReturn (document );
87
92
88
- MvcResult mvcResult = mockMvc .perform (delete ("/docs/{name}/delete" , document . getName () ))
93
+ MvcResult mvcResult = mockMvc .perform (delete ("/docs/{name}/delete" , name ))
89
94
.andExpect (status ().isOk ()).andReturn ();
90
95
91
96
HashMap <String , Document > response = objectMapper
92
97
.readValue (mvcResult .getResponse ().getContentAsString (),
93
98
new TypeReference <HashMap <String , Document >>() {
94
99
});
95
100
96
- assertEquals (response .get ("deleted" ).getName (), document .getName ());
97
-
101
+ assertEquals (response .get ("deleted" ).getName (), name );
102
+ then (documentsService ).should ().removeDocument (name );
103
+ then (documentsService ).should ().getDocument (name );
98
104
99
105
}
100
106
@@ -112,18 +118,23 @@ void createDocumentTest() throws Exception {
112
118
Document addedDocument = objectMapper .readValue (mvcResult .getResponse ().getContentAsString (), Document .class );
113
119
114
120
assertEquals (document , addedDocument );
121
+
122
+ then (documentsService ).should ().addDocument (document );
115
123
}
116
124
117
125
@ Test
118
126
void renameTest () throws Exception {
119
127
Document document = testDocumentFactory .get ();
120
128
String newName = "Foo" ;
121
129
122
- given (documentsService .changeDocumentsName (document .getName (), newName )).willReturn (document );
130
+ String name = document .getName ();
131
+ given (documentsService .changeDocumentsName (name , newName )).willReturn (document );
123
132
124
- mockMvc .perform (patch ("/docs/{name}" , document . getName () ).param ("newName" , newName ))
133
+ mockMvc .perform (patch ("/docs/{name}" , name ).param ("newName" , newName ))
125
134
.andExpect (status ().isOk ()).andReturn ();
126
135
136
+ then (documentsService ).should ().changeDocumentsName (name , newName );
137
+
127
138
}
128
139
129
140
0 commit comments