????????????????????ò????·
???????????? ???????[ 2015/11/26 14:09:50 ] ??????????????? ???????
??????????????????????????飬??Щ?????????????????????????е????????????????????????????????????????
????????????????????????????????????????????……???в???????????Щ?????????á?
??????????????????????????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?????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11