Skip to content

Commit

Permalink
springframeworkguru#8 : Spring MVC Mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Michiel Van Huyck committed Sep 20, 2022
1 parent 39fd847 commit dc0b2cd
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.model.Vets;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -19,6 +21,9 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@ExtendWith(MockitoExtension.class)
class VetControllerTest {
Expand All @@ -34,13 +39,25 @@ class VetControllerTest {

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

MockMvc mockMvc;

@BeforeEach
void setUp() {

//given
vetsList.add(new Vet());

given(clinicService.findVets()).willReturn(vetsList);

mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}

@Test
void testControllerShowVetList() throws Exception {
mockMvc.perform(get("/vets.html"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("vets")) //gets the model
.andExpect(view().name("vets/vetList")); //gets the view
}

@Test
Expand Down

0 comments on commit dc0b2cd

Please sign in to comment.