Unit Test
VetController
@Controller
public class VetController {
private final ClinicService clinicService;
@Autowired
public VetController(ClinicService clinicService) {this.clinicService = clinicService;}
@RequestMapping(value = { "/vets.html"})
public String showVetList(Map<String, Object> model) {
// Here we are returning an object of type 'Vets' rather than a collection of Vet objects
// so it is simpler for Object-Xml mapping
Vets vets = new Vets();
vets.getVetList().addAll(this.clinicService.findVets());
model.put("vets", vets);
return "vets/vetList";
}
}
VetControllerTest
//...
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
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.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@ExtendWith(MockitoExtension.class)
class VetControllerTest {
@Mock
ClinicService clinicService;
@Mock
Map<String, Object> model;
@InjectMocks
VetController controller;
List<Vet> vetsList = new ArrayList<>();
MockMvc mockMvc;
@BeforeEach
void setUp() {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"))
.andExpect(view().name("vets/vetList"));
}
}
Integration Test
OwnerController
@Controller
public class OwnerController {
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
private final ClinicService clinicService;
@RequestMapping(value = "/owners/new", method = RequestMethod.GET)
public String initCreationForm(Map<String, Object> model) {Owner owner = new Owner();
model.put("owner", owner);
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
}
OwnerControllerTest
// ...
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringJUnitWebConfig(locations = {"classpath:spring/mvc-test-config.xml",
"classpath:spring/mvc-core-config.xml"})
// @SpringJUnitWebConfig({Abc.class, Uvw.class})
class OwnerControllerTest {
@Autowired
OwnerController ownerController;
@Autowired
ClinicService clinicService;
MockMvc mockMvc;
@BeforeEach
void setUp() {mockMvc = MockMvcBuilders.standaloneSetup(ownerController).build();}
@Test
void initCreationFormTest() throws Exception {mockMvc.perform(get("/owners/new"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("owner"))
.andExpect(view().name("owners/createOrUpdateOwnerForm"));
}
}