Ê×Ò³/¼¼Êõ¿ª·¢/ÄÚÈÝ

JUnitʵʩ--3,4,5

¼¼Êõ¿ª·¢2023-12-17 ÔĶÁ()
3.Design by Contract£¨Õâ¾ä»°ÎÒû·¨·­Ò룩

Design by Contract±¾ÊÇBertrand Meyer£¨EiffelÓïÑԵĴ´Ê¼ÈË£©¿ª·¢µÄÒ»ÖÖÉè¼Æ¼¼Êõ¡£ÎÒ·¢ÏÖÔÚJUnitÖÐʹÓÃDesign by Contract»á´øÀ´ÒâÏë²»µ½µÄЧ¹û¡£Design by ContractµÄºËÐÄÊǶÏÑÔ£¨assersion£©¡£¶ÏÑÔÊÇÒ»¸ö²¼¶ûÓï¾ä£¬¸ÃÓï¾ä²»ÄÜΪ¼Ù£¬Èç¹ûΪ¼Ù£¬Ôò±íÃ÷³öÏÖÁËÒ»¸öbug¡£Design by ContractʹÓÃÈýÖÖ¶ÏÑÔ£ºÇ°ÖÃÌõ¼þ£¨pre-conditions£©¡¢ºóÖÃÌõ¼þ£¨post-conditions£©ºÍ²»±äʽ£¨invariants£©ÕâÀï²»´òËãÏêϸÌÖÂÛDesign by ContractµÄϸ½Ú£¬¶øÊÇÏ£ÍûÆäÔÚ²âÊÔÖÐÄÜ·¢»ÓÆä×÷Óá£

Ç°ÖÃÌõ¼þÔÚÖ´ÐвâÊÔ֮ǰ¿ÉÒÔÓÃÓÚÅжÏÊÇ·ñÔÊÐí½øÈë²âÊÔ£¬¼´½øÈë²âÊÔµÄÌõ¼þ¡£Èç expectedWheels > 0, myCar != null¡£ºóÖÃÌõ¼þÓÃÓÚÔÚ²âÊÔÖ´ÐкóÅжϲâÊԵĽá¹ûÊÇ·ñÕýÈ·¡£Èç expectedWheels==myCar.getWheels()¡£¶ø²»±äʽÔÚÅжϽ»Ò×(Transaction)µÄÒ»ÖÂÐÔ£¨consistency£©·½ÃæÓÈΪÓÐÓá£ÎÒÏ£ÍûJUnit¿ÉÒÔ½«Design by Contract×÷ΪδÀ´°æ±¾µÄÒ»¸öÔöÇ¿¡£


4.Refactoring£¨Õâ¾ä»°ÎÒÒÀȻû·¨·­Ò룩

Refactoring±¾À´Óë²âÊÔûÓÐÖ±½ÓµÄÁªÏµ£¬¶øÊÇÓëÈí¼þìØÓйأ¬µ«¼ÈÈ»ÎÒÃÇ˵²âÊÔÄܽâ¾öÈí¼þìØÎÊÌ⣬ÎÒÃÇÒ²¾Í±ØÐë˵³ö½â¾öÖ®µÀ¡££¨½ö½ö½øÐвâÊÔÖ»ÄÜ·¢ÏÖÈí¼þìØ£¬RefactoringÔò¿É½â¾öÈí¼þìØ´øÀ´µÄÎÊÌâ¡££©Èí¼þìØÒý³öÁËÒ»¸öÎÊÌ⣺ÊÇ·ñÐèÒªÖØÐÂÉè¼ÆÕû¸öÈí¼þµÄ½á¹¹£¿ÀíÂÛÉÏÓ¦¸ÃÈç´Ë£¬µ«ÏÖʵ²»ÔÊÐíÎÒÃÇÕâô×ö¡£Õâ»òÕßÊÇÓÉÓÚʱ¼äµÄÔ­Òò£¬»òÕßÊÇÓÉÓÚ·ÑÓõÄÔ­Òò¡£ÖØÐÂÉè¼ÆÕû¸öÈí¼þµÄ½á¹¹»á¸øÎÒÃÇ´øÀ´¶ÌÆÚµÄÍ´¿à¡£¶ø²»Í£µØ¸øÈí¼þ´ò²¹¶¡ÉõÖÁÊDz¹¶¡µÄ²¹¶¡Ôò»á¸øÎÒÃÇ´øÀ´³¤ÆÚµÄÍ´¿à¡££¨²»¹ÜÔõÑù£¬ÎÒÃÇ×Ü´¦ÓÚË®Éî»ðÈÈÖ®ÖУ©

RefactoringÊÇÒ»¸öÊõÓÓÃÓÚÃèÊöÒ»ÖÖ¼¼Êõ£¬ÀûÓÃÕâÖÖ¼¼ÊõÎÒÃÇ¿ÉÒÔÃâÓÚÖع¹Õû¸öÈí¼þËù´øÀ´µÄ¶ÌÆÚÍ´¿à¡£µ±Äãrefactorʱ£¬Äã²¢²»¸Ä±ä³ÌÐòµÄ¹¦ÄÜ£¬¶øÊǸıä³ÌÐòÄÚ²¿µÄ½á¹¹£¬Ê¹Æä¸üÒ×Àí½âºÍʹÓá£È磺¸Ã±äÒ»¸ö·½·¨µÄÃû×Ö£¬½«Ò»¸ö³ÉÔ±±äÁ¿´ÓÒ»¸öÀàÒƵ½ÁíÒ»¸öÀ࣬½«Á½¸öÀàËÆ·½·¨³éÏ󵽸¸ÀàÖС£Ëù×÷µÄÿһ¸ö²½¶¼ºÜС£¬È»¶ø1£­2¸öСʱµÄRefactoring¹¤×÷¿ÉÒÔʹÄãµÄ³ÌÐò½á¹¹¸üÊʺÏÄ¿Ç°µÄÇé¿ö¡£RefactoringÓÐһЩ¹æÔò£º

1> ²»ÒªÔÚ¼ÓÈëй¦ÄܵÄͬʱrefactorÒÑÓеĴúÂë¡£ÔÚÕâÁ½Õß¼äÒªÓÐÒ»¸öÇåÎúµÄ½çÏÞ¡£ÈçÿÌìÔçÉÏ1-2¸öСʱµÄRefactoring£¬ÆäÓàʱ¼äÌí¼ÓÐµĹ¦ÄÜ¡£

2> ÔÚÄ㿪ʼRefactoringÇ°£¬ºÍRefactoringºó¶¼Òª±£Ö¤²âÊÔÄÜ˳Àûͨ¹ý¡£·ñÔòRefactoringûÓÐÈκÎÒâÒå¡£

3> ½øÐÐСµÄRefactoring£¬´óµÄ¾Í²»ÊÇRefactoringÁË¡£Èç¹ûÄã´òËãÖع¹Õû¸öÈí¼þ£¬¾ÍûÓбØÒªRefactoringÁË¡£

Ö»ÓÐÔÚÌí¼Óй¦Äܺ͵÷ÊÔbugʱ²ÅÓÖ±ØÒªRefactoring¡£²»ÒªµÈµ½½»¸¶Èí¼þµÄ×îºó¹ØÍ·²ÅRefactoring¡£ÄÇÑùºÍ´ò²¹¶¡µÄÇø±ð²»´ó¡£Refactoring ÓÃÔڻعé²âÊÔÖÐÒ²ÄÜÏÔʾÆäÍþÁ¦¡£ÒªÃ÷°×£¬ÎÒ²»·´¶Ô´ò²¹¶¡£¬µ«Òª¼Çס´ò²¹¶¡ÊÇÓ¦¸Ã×îºóʹÓõıØɱ¾øÕС££¨´ò²¹¶¡Ò²ÐèÒªºÜ¸ßµÄ¼¼Êõ£¬ÏêÇé²Î¿´Î¢ÈíÍøÕ¾£©

5.IDE¶ÔJUnitµÄÖ§³Ö


Ä¿Ç°Ö§³ÖJUnitµÄJava IDE °üÀ¨

  • IDE


  • ……

    Ïà¹ØÔĶÁ