2424import java .util .function .Supplier ;
2525
2626import 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 ;
2929import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
3030import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
3131import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
@@ -52,13 +52,16 @@ void getDocumentTest() throws Exception {
5252
5353 Document document = testDocumentFactory .get ();
5454
55- given (documentsService .getDocument (document .getName ())).willReturn (document );
55+ String name = document .getName ();
56+ given (documentsService .getDocument (name )).willReturn (document );
5657
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 ))
5960 .andExpect (jsonPath ("$.content" ).value (document .getContent ()))
6061 .andExpect (jsonPath ("$.id" ).value (document .getId ())).andReturn ();
6162
63+ then (documentsService ).should ().getDocument (name );
64+
6265 }
6366
6467 @ Test
@@ -75,26 +78,29 @@ void getAllDocumentsTest() throws Exception {
7578 });
7679
7780 assertEquals (documents , documentsFromResponse );
81+ then (documentsService ).should ().getAllDocuments ();
7882 }
7983
8084 @ Test
8185 void deleteDocumentTest () throws Exception {
8286
8387 Document document = testDocumentFactory .get ();
8488
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 );
8792
88- MvcResult mvcResult = mockMvc .perform (delete ("/docs/{name}/delete" , document . getName () ))
93+ MvcResult mvcResult = mockMvc .perform (delete ("/docs/{name}/delete" , name ))
8994 .andExpect (status ().isOk ()).andReturn ();
9095
9196 HashMap <String , Document > response = objectMapper
9297 .readValue (mvcResult .getResponse ().getContentAsString (),
9398 new TypeReference <HashMap <String , Document >>() {
9499 });
95100
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 );
98104
99105 }
100106
@@ -112,18 +118,23 @@ void createDocumentTest() throws Exception {
112118 Document addedDocument = objectMapper .readValue (mvcResult .getResponse ().getContentAsString (), Document .class );
113119
114120 assertEquals (document , addedDocument );
121+
122+ then (documentsService ).should ().addDocument (document );
115123 }
116124
117125 @ Test
118126 void renameTest () throws Exception {
119127 Document document = testDocumentFactory .get ();
120128 String newName = "Foo" ;
121129
122- given (documentsService .changeDocumentsName (document .getName (), newName )).willReturn (document );
130+ String name = document .getName ();
131+ given (documentsService .changeDocumentsName (name , newName )).willReturn (document );
123132
124- mockMvc .perform (patch ("/docs/{name}" , document . getName () ).param ("newName" , newName ))
133+ mockMvc .perform (patch ("/docs/{name}" , name ).param ("newName" , newName ))
125134 .andExpect (status ().isOk ()).andReturn ();
126135
136+ then (documentsService ).should ().changeDocumentsName (name , newName );
137+
127138 }
128139
129140
0 commit comments