??????????????????????????飬??Щ?????????????????????????е????????????????????????????????????????
????????????????????????????????????????????……???в???????????Щ?????????á?
??????????????????????????Java?????У???????????“????????”????????????????????????????????????????????Spring????????Pet Clinic?е?????????????????PetController????????????????????????????
????@Controller
????@SessionAttributes("pet")
????public class PetController {
????private final ClinicService clinicService;
????@Autowired
????public PetController(ClinicService clinicService) {
????this.clinicService = clinicService;
????}
????@ModelAttribute("types")
????public Collection<PetType> populatePetTypes() {
????return this.clinicService.findPetTypes();
????}
????@RequestMapping(value = "/owners/{ownerId}/pets/new"?? method = RequestMethod.GET)
????public String initCreationForm(@PathVariable("ownerId") int ownerId??
????Map<String?? Object> model) {
????Owner owner = this.clinicService.findOwnerById(ownerId);
????Pet pet = new Pet();
????owner.addPet(pet);
????model.put("pet"?? pet);
????return "pets/createOrUpdatePetForm";
????}
????...
????}??????????????initCreationForm()???????????????????????μ?Pet??????????????????????÷??????????
??????model?з??????Pet?????
?????????Pet?????Owner????
????????????????????
?????????????????

?????????????????????????????????????????????????е???????????????????????е?Mockito?????????????????????
????public class PetControllerTest {
????private ClinicService clinicService;
????private PetController controller;
????@Before
????public void setUp() {
????clinicService = Mockito.mock(ClinicService.class);
????controller = new PetController(clinicService);
????}
????@Test
????public void should_set_pet_in_model() {
????Owner dummyOwner = new Owner();
????Mockito.when(clinicService.findOwnerById(1)).thenReturn(dummyOwner);
????HashMap<String?? Object> model = new HashMap<String?? Object>();
????controller.initCreationForm(1?? model);
????Iterator<Object> modelIterator = model.values().iterator();
????Assert.assertTrue(modelIterator.hasNext());
????Object value = modelIterator.next();
????Assert.assertTrue(value instanceof Pet);
????Pet pet = (Pet) value;
????Owner petOwner = pet.getOwner();
????Assert.assertNotNull(petOwner);
????Assert.assertSame(dummyOwner?? petOwner);
????}
????}setUp()????????controller???г?????????в??????????????ClinicService?????????Mockito?????mock??????????????????
????should_set_pet_in_model()????????????????????巽?????к???model??????????Pet???????????????Owner?????mock??ClinicService???????Owner?????