mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-20 14:55:50 +00:00
Add unit test for VetController.showResourcesVetList
This commit is contained in:
parent
0a7e1ae9dd
commit
625eb7ef14
1 changed files with 54 additions and 0 deletions
|
@ -130,3 +130,57 @@ class VetControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
package org.springframework.samples.petclinic.vet;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
class VetControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private VetRepository vetRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private VetController vetController;
|
||||||
|
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
MockitoAnnotations.openMocks(this);
|
||||||
|
mockMvc = MockMvcBuilders.standaloneSetup(vetController).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testShowResourcesVetList() throws Exception {
|
||||||
|
Vet vet1 = new Vet();
|
||||||
|
vet1.setId(1);
|
||||||
|
vet1.setFirstName("James");
|
||||||
|
vet1.setLastName("Carter");
|
||||||
|
Vet vet2 = new Vet();
|
||||||
|
vet2.setId(2);
|
||||||
|
vet2.setFirstName("Helen");
|
||||||
|
vet2.setLastName("Leary");
|
||||||
|
|
||||||
|
given(vetRepository.findAll()).willReturn(Arrays.asList(vet1, vet2));
|
||||||
|
|
||||||
|
mockMvc.perform(get("/vets")
|
||||||
|
.accept(MediaType.APPLICATION_JSON))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||||
|
.andExpect(content().json("{"vetList":[{"id":1,"firstName":"James","lastName":"Carter","specialties":[]},{"id":2,"firstName":"Helen","lastName":"Leary","specialties":[]}]}", true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue