11import com .bobocode .mvc .HelloSpringMvcApp ;
2- import com .bobocode .mvc .model . Note ;
2+ import com .bobocode .mvc .controller . NoteController ;
33import com .bobocode .mvc .data .Notes ;
4+ import com .bobocode .mvc .model .Note ;
45import org .junit .jupiter .api .Test ;
56import org .springframework .beans .factory .annotation .Autowired ;
67import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
8+ import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
79import org .springframework .boot .test .context .SpringBootTest ;
10+ import org .springframework .boot .test .mock .mockito .MockBean ;
811import org .springframework .test .web .servlet .MockMvc ;
912import 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 ;
1318import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
1419import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
15- import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
1620import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
1721
18- @ SpringBootTest (classes = HelloSpringMvcApp .class )
1922@ AutoConfigureMockMvc
23+ @ SpringBootTest (classes = HelloSpringMvcApp .class )
2024public 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}
0 commit comments