{"id":106106,"date":"2024-04-16T17:35:01","date_gmt":"2024-04-16T09:35:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/106106.html"},"modified":"2024-04-16T17:35:03","modified_gmt":"2024-04-16T09:35:03","slug":"%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e4%b8%ad%e7%9a%84%e9%9a%94%e7%a6%bb%e7%ba%a7%e5%88%ab%e6%80%8e%e6%a0%b7%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/106106.html","title":{"rendered":"\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9694\u79bb\u7ea7\u522b\u600e\u6837\u8bbe\u7f6e"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02173615\/7b90f2c5-1299-41f2-8110-3154017d45c2.webp\" alt=\"\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9694\u79bb\u7ea7\u522b\u600e\u6837\u8bbe\u7f6e\" \/><\/p>\n<p><p>\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9694\u79bb\u7ea7\u522b\u8bbe\u7f6e\u4f9d\u8d56\u4e8e\u6d4b\u8bd5\u73af\u5883\u3001\u6846\u67b6\u548c\u6240\u9700\u7684\u6d4b\u8bd5\u51c6\u786e\u5ea6\u3002<strong>\u9694\u79bb\u7ea7\u522b\u662f\u6307\u6d4b\u8bd5\u7528\u4f8b\u6267\u884c\u65f6\u5bf9\u5176\u5b83\u6d4b\u8bd5\u7528\u4f8b\u3001\u7cfb\u7edf\u72b6\u6001\u548c\u5916\u90e8\u8d44\u6e90\u7684\u5f71\u54cd\u7a0b\u5ea6<\/strong>\u3002\u8bbe\u7f6e\u5408\u9002\u7684\u9694\u79bb\u7ea7\u522b\u53ef\u4ee5\u786e\u4fdd\u6d4b\u8bd5\u7ed3\u679c\u7684\u53ef\u91cd\u590d\u6027\u548c\u51c6\u786e\u6027\uff0c\u5e38\u7528\u7684\u9694\u79bb\u7ea7\u522b\u5305\u62ec<strong>\u6ca1\u6709\u9694\u79bb\u3001\u7c7b\u7ea7\u522b\u9694\u79bb\u3001\u65b9\u6cd5\u7ea7\u522b\u9694\u79bb\u3001\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u7ea7\u522b\u9694\u79bb<\/strong>\u3002\u9488\u5bf9\u65b9\u6cd5\u7ea7\u522b\u9694\u79bb\uff0c\u6d89\u53ca\u5230\u6bcf\u4e00\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u5728\u6267\u884c\u65f6\u90fd\u62e5\u6709\u81ea\u5df1\u7684\u8d44\u6e90\u548c\u73af\u5883\u526f\u672c\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6d4b\u8bd5\u6846\u67b6\u63d0\u4f9b\u7684\u529f\u80fd\u6216\u4f7f\u7528\u6a21\u62df(mocking)\u548c\u5b58\u6839(stubbing)\u6280\u672f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6ca1\u6709\u9694\u79bb<\/p>\n<p>\u5728\u6ca1\u6709\u9694\u79bb\u7ea7\u522b\u7684\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u7684\u5355\u5143\u6d4b\u8bd5\u5171\u4eab\u76f8\u540c\u7684\u73af\u5883\u548c\u8d44\u6e90\u3002\u8fd9\u610f\u5473\u7740\u524d\u4e00\u4e2a\u6d4b\u8bd5\u7684\u7ed3\u679c\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u540e\u4e00\u4e2a\u6d4b\u8bd5\u7684\u884c\u4e3a\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u5728\u8d44\u6e90\u5229\u7528\u4e0a\u6700\u9ad8\u6548\uff0c\u4f46\u5374\u5f88\u5bb9\u6613\u51fa\u73b0\u6d4b\u8bd5\u95f4\u7684\u5e72\u6270\u548c\u4f9d\u8d56\uff0c\u8fd9\u662f\u56e0\u4e3a\u6d4b\u8bd5\u72b6\u6001\u4e0d\u4f1a\u5728\u6bcf\u6b21\u6d4b\u8bd5\u4e4b\u95f4\u91cd\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u7c7b\u7ea7\u522b\u9694\u79bb<\/p>\n<p>\u7c7b\u7ea7\u522b\u9694\u79bb\u610f\u5473\u7740\u5728\u540c\u4e00\u4e2a\u6d4b\u8bd5\u7c7b\u4e2d\u7684\u65b9\u6cd5\u53ef\u4ee5\u5171\u4eab\u7c7b\u7684\u5b9e\u4f8b\u72b6\u6001\uff0c\u4f46\u662f\u4e0d\u540c\u6d4b\u8bd5\u7c7b\u7684\u5b9e\u4f8b\u72b6\u6001\u662f\u9694\u79bb\u7684\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u51cf\u5c11\u6d4b\u8bd5\u4e4b\u95f4\u7684\u5e72\u6270\uff0c\u540c\u65f6\u4ecd\u5171\u4eab\u540c\u4e00\u7c7b\u5b9e\u4f8b\u4e2d\u7684\u8d44\u6e90\uff0c\u8fd9\u4fdd\u6301\u4e86\u6d4b\u8bd5\u73af\u5883\u8f83\u9ad8\u7684\u91cd\u7528\u6027\u3002\u5728\u5f88\u591a\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u4e2d\u90fd\u6709\u652f\u6301\uff0c\u901a\u5e38\u5728\u6d4b\u8bd5\u7c7b\u7684\u521d\u59cb\u5316\u548c\u6e05\u7406\u65f6\u8fdb\u884c\u8d44\u6e90\u914d\u7f6e\u548c\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u65b9\u6cd5\u7ea7\u522b\u9694\u79bb<\/p>\n<p>\u65b9\u6cd5\u7ea7\u522b\u9694\u79bb\u4fdd\u8bc1\u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u90fd\u5728\u4e00\u4e2a\u5e72\u51c0\u7684\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u6bcf\u6b21\u6d4b\u8bd5\u524d\u540e\u90fd\u6709\u72ec\u7acb\u7684\u8bbe\u7f6e\u548c\u6e05\u7406\u6b65\u9aa4\u3002\u8fd9\u662f\u4e00\u79cd\u8f83\u4e3a\u5e38\u89c1\u7684\u8bbe\u7f6e\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u660e\u663e\u964d\u4f4e\u6d4b\u8bd5\u95f4\u7684\u5e72\u6270\uff0c\u4f46\u662f\u4ee3\u4ef7\u662f\u9700\u8981\u66f4\u591a\u7684\u8d44\u6e90\u521d\u59cb\u5316\u548c\u6e05\u7406\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6d4b\u8bd5\u7684\u6267\u884c\u901f\u5ea6\u964d\u4f4e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u7ea7\u522b\u9694\u79bb<\/p>\n<p>\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u7ea7\u522b\u9694\u79bb\u63d0\u4f9b\u4e86\u6700\u9ad8\u7a0b\u5ea6\u7684\u9694\u79bb\uff0c\u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u90fd\u5728\u5355\u72ec\u7684\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u3002\u8fd9\u79cd\u9694\u79bb\u7ea7\u522b\u9002\u7528\u4e8e\u5bf9\u9694\u79bb\u6027\u8981\u6c42\u6781\u9ad8\u7684\u6d4b\u8bd5\u73af\u5883\uff0c\u4f8b\u5982\u9700\u8981\u6d4b\u8bd5\u591a\u7ebf\u7a0b\u5b89\u5168\u7684\u4ee3\u7801\u3002\u7136\u800c\uff0c\u8fd9\u4e5f\u662f\u5f00\u9500\u6700\u5927\u7684\u9694\u79bb\u65b9\u5f0f\uff0c\u56e0\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u4e3a\u6bcf\u4e2a\u6d4b\u8bd5\u5206\u914d\u72ec\u7acb\u7684\u7cfb\u7edf\u8d44\u6e90\u548cCPU\u65f6\u95f4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4ec0\u4e48\u662f\u5355\u5143\u6d4b\u8bd5\u7684\u9694\u79bb\u7ea7\u522b\uff1f\u5982\u4f55\u8bbe\u7f6e\u9694\u79bb\u7ea7\u522b\uff1f<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u7684\u9694\u79bb\u7ea7\u522b\u6307\u7684\u662f\u5728\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u65f6\uff0c\u5982\u4f55\u5c06\u88ab\u6d4b\u8bd5\u7684\u4ee3\u7801\u4e0e\u5176\u4ed6\u4f9d\u8d56\u7ec4\u4ef6\u8fdb\u884c\u9694\u79bb\uff0c\u4ee5\u786e\u4fdd\u6d4b\u8bd5\u7ed3\u679c\u7684\u51c6\u786e\u6027\u548c\u53ef\u91cd\u590d\u6027\u3002\u8bbe\u7f6e\u9694\u79bb\u7ea7\u522b\u9700\u8981\u8003\u8651\u6d4b\u8bd5\u5bf9\u8c61\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u4ee5\u53ca\u6d4b\u8bd5\u76ee\u7684\u548c\u73af\u5883\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u9002\u5f53\u7684\u5355\u5143\u6d4b\u8bd5\u9694\u79bb\u7ea7\u522b\uff1f<\/strong><\/p>\n<p>\u9009\u62e9\u9002\u5f53\u7684\u5355\u5143\u6d4b\u8bd5\u9694\u79bb\u7ea7\u522b\u5e94\u6839\u636e\u88ab\u6d4b\u8bd5\u4ee3\u7801\u7684\u4f9d\u8d56\u5173\u7cfb\u548c\u6d4b\u8bd5\u8981\u6c42\u6765\u51b3\u5b9a\u3002\u5e38\u7528\u7684\u5355\u5143\u6d4b\u8bd5\u9694\u79bb\u7ea7\u522b\u5305\u62ec\u201c\u65e0\u4f9d\u8d56\u9694\u79bb\u201d\u3001\u201c\u6869\u5bf9\u8c61\u9694\u79bb\u201d\u548c\u201c\u6a21\u62df\u5bf9\u8c61\u9694\u79bb\u201d\u7b49\u3002\u6839\u636e\u5177\u4f53\u60c5\u51b5\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6d4b\u8bd5\u76ee\u7684\u9009\u62e9\u9002\u5408\u7684\u9694\u79bb\u7ea7\u522b\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e\u5355\u5143\u6d4b\u8bd5\u7684\u9694\u79bb\u7ea7\u522b\uff1f<\/strong><\/p>\n<p>\u8bbe\u7f6e\u5355\u5143\u6d4b\u8bd5\u7684\u9694\u79bb\u7ea7\u522b\u9700\u8981\u901a\u8fc7\u4f7f\u7528\u9002\u5f53\u7684\u5de5\u5177\u548c\u6280\u672f\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Mockito\u3001PowerMock\u6216EasyMock\u7b49\u5de5\u5177\u53ef\u4ee5\u521b\u5efa\u6869\u5bf9\u8c61\u6216\u6a21\u62df\u5bf9\u8c61\u6765\u5bf9\u88ab\u6d4b\u8bd5\u5bf9\u8c61\u7684\u4f9d\u8d56\u8fdb\u884c\u9694\u79bb\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u3001\u63a5\u53e3\u9694\u79bb\u6216\u6a21\u5757\u5316\u8bbe\u8ba1\u7b49\u6280\u672f\u6765\u964d\u4f4e\u4f9d\u8d56\u8026\u5408\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u597d\u7684\u9694\u79bb\u6548\u679c\u3002\u5728\u8bbe\u7f6e\u9694\u79bb\u7ea7\u522b\u65f6\uff0c\u9700\u8003\u8651\u6d4b\u8bd5\u8986\u76d6\u8303\u56f4\u3001\u6d4b\u8bd5\u6027\u80fd\u548c\u6d4b\u8bd5\u53ef\u7ef4\u62a4\u6027\u7b49\u65b9\u9762\u7684\u56e0\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9694\u79bb\u7ea7\u522b\u8bbe\u7f6e\u4f9d\u8d56\u4e8e\u6d4b\u8bd5\u73af\u5883\u3001\u6846\u67b6\u548c\u6240\u9700\u7684\u6d4b\u8bd5\u51c6\u786e\u5ea6\u3002\u9694\u79bb\u7ea7\u522b\u662f\u6307\u6d4b\u8bd5\u7528\u4f8b\u6267\u884c\u65f6\u5bf9\u5176\u5b83\u6d4b\u8bd5\u7528\u4f8b\u3001\u7cfb\u7edf [&hellip;]","protected":false},"author":3,"featured_media":106109,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/106106"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=106106"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/106106\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/106109"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=106106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=106106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=106106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}