Skip to content

Commit dc0b2cd

Browse files
author
Michiel Van Huyck
committed
springframeworkguru#8 : Spring MVC Mock
1 parent 39fd847 commit dc0b2cd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/test/java/org/springframework/samples/petclinic/web/VetControllerTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.springframework.samples.petclinic.model.Vet;
1010
import org.springframework.samples.petclinic.model.Vets;
1111
import org.springframework.samples.petclinic.service.ClinicService;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1214

1315
import java.util.ArrayList;
1416
import java.util.List;
@@ -19,6 +21,9 @@
1921
import static org.mockito.ArgumentMatchers.anyString;
2022
import static org.mockito.BDDMockito.given;
2123
import static org.mockito.BDDMockito.then;
24+
import static org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.*;
25+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2227

2328
@ExtendWith(MockitoExtension.class)
2429
class VetControllerTest {
@@ -34,13 +39,25 @@ class VetControllerTest {
3439

3540
List<Vet> vetsList = new ArrayList<>();
3641

42+
MockMvc mockMvc;
43+
3744
@BeforeEach
3845
void setUp() {
3946

4047
//given
4148
vetsList.add(new Vet());
4249

4350
given(clinicService.findVets()).willReturn(vetsList);
51+
52+
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
53+
}
54+
55+
@Test
56+
void testControllerShowVetList() throws Exception {
57+
mockMvc.perform(get("/vets.html"))
58+
.andExpect(status().isOk())
59+
.andExpect(model().attributeExists("vets")) //gets the model
60+
.andExpect(view().name("vets/vetList")); //gets the view
4461
}
4562

4663
@Test

0 commit comments

Comments
 (0)