{"id":6863,"date":"2022-08-03T20:49:30","date_gmt":"2022-08-03T12:49:30","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6863"},"modified":"2025-08-07T09:44:25","modified_gmt":"2025-08-07T01:44:25","slug":"%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6863","title":{"rendered":"\u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5"},"content":{"rendered":"\n<p><strong>\u4e00\u3001\u6982\u8ff0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-29-1024x584.png\" alt=\"\" class=\"wp-image-6864\" width=\"488\" height=\"278\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-29-1024x584.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-29-300x171.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-29-768x438.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-29-830x474.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-29-230x131.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-29-350x200.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-29-480x274.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-29.png 1244w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/figure><\/div>\n\n\n\n<p>\u5355\u5143\u6d4b\u8bd5\u662f\u6307\u5bf9\u8f6f\u4ef6\u4e2d\u7684\u6700\u5c0f\u53ef\u6d4b\u8bd5\u5355\u5143\u8fdb\u884c\u68c0\u67e5\u548c\u9a8c\u8bc1\u3002\u5355\u5143\u5728\u8d28\u91cf\u4fdd\u8bc1\u4e2d\u662f\u975e\u5e38\u91cd\u8981\u7684\u73af\u8282\uff0c\u6839\u636e\u6d4b\u8bd5\u91d1\u5b57\u5854\u539f\u7406\uff0c\u8d8a\u5f80\u4e0a\u5c42\u7684\u6d4b\u8bd5\uff0c\u6240\u9700\u7684\u6d4b\u8bd5\u6295\u5165\u6bd4\u4f8b\u8d8a\u5927\uff0c\u6548\u679c\u4e5f\u8d8a\u5dee\uff0c\u800c\u5355\u5143\u6d4b\u8bd5\u7684\u6210\u672c\u8981\u5c0f\u7684\u591a\uff0c\u4e5f\u66f4\u5bb9\u6613\u53d1\u73b0\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>1.1 \u5355\u5143\u6d4b\u8bd5\u67b6\u6784<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-30.png\" alt=\"\" class=\"wp-image-6865\" width=\"519\" height=\"344\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-30.png 982w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-30-300x199.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-30-768x510.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-30-830x551.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-30-230x153.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-30-350x232.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-30-480x319.png 480w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ee5\u6709\u67d0\u5e94\u7528\u4e3a\u4f8b\uff0c\u5e94\u7528\u90e8\u7f72\u662f\u5fae\u670d\u52a1\u67b6\u6784\uff0c\u5bf9\u5916\u63d0\u4f9bdubbo\u670d\u52a1\uff0c\u5f53\u524d\u7684\u5355\u5143\u6d4b\u8bd5\uff0c\u91c7\u7528\u4e86\u5206\u5c42\u6d4b\u8bd5\u6846\u67b6\uff0c\u6839\u636e\u4ee3\u7801\u7684\u5206\u5c42\uff0c\u5206\u4e3a Service \u5c42\u6d4b\u8bd5\uff0cBiz \u5c42\u6d4b\u8bd5\uff0c\u5916\u90e8\u670d\u52a1\u8bbf\u95ee\u5c42\u6d4b\u8bd5\uff0cDAO \u6d4b\u8bd5\uff0c<a rel=\"noreferrer noopener\" href=\"https:\/\/cloud.tencent.com\/product\/crs?from=10680\" target=\"_blank\">Redis<\/a> \u8bbf\u95ee\u5c42\u6d4b\u8bd5\uff0c\u6bcf\u4e00\u5c42\u5747\u4f7f\u7528 mock \u6846\u67b6\u5c4f\u853d\u4e0b\u5c42\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p><strong>1.2 \u5355\u5143\u6d4b\u8bd5\u7684\u8fc7\u7a0b<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-31-1024x82.png\" alt=\"\" class=\"wp-image-6866\" width=\"609\" height=\"49\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-31-1024x82.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-31-300x24.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-31-768x61.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-31-830x66.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-31-230x18.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-31-350x28.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-31-480x38.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-31.png 1480w\" sizes=\"(max-width: 609px) 100vw, 609px\" \/><\/figure><\/div>\n\n\n\n<p>\u5355\u5143\u6d4b\u8bd5\u7684\u7f16\u5199\uff0c\u4e3b\u8981\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u9636\u6bb5\uff1a<\/p>\n\n\n\n<ul><li>\u6570\u636e\u51c6\u5907\uff1a\u5728\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u524d\uff0c\u9700\u8981\u4f9d\u8d56\u5230\u4e00\u4e9b\u6570\u636e\uff0c\u6570\u636e\u6765\u6e90\u4e00\u822c\u662f<a rel=\"noreferrer noopener\" href=\"https:\/\/cloud.tencent.com\/solution\/database?from=10680\" target=\"_blank\">\u6570\u636e\u5e93<\/a>\uff0c\u800c\u6784\u9020\u6570\u636e\uff0c\u53c8\u4e0d\u80fd\u4f9d\u8d56 DAO \u5c42\u7684\u4ee3\u7801\uff0c\u9700\u8981\u4f7f\u7528\u539f\u751fjdbc \u53bb\u63d2\u5165\u6570\u636e\uff0c\u6d4b\u8bd5\u4ee3\u7801\u7f16\u5199\u6548\u7387\u4f4e\u3002<\/li><li>\u6784\u9020\u53c2\u6570\u53ca\u6253\u6869\uff08stub\uff09\uff1a\u8c03\u7528\u65b9\u6cd5\u9700\u8981\u4f20\u9012\u5165\u53c2\uff0c\u6709\u65f6\u5019\u4e00\u4e2a\u5165\u53c2\u5341\u51e0\u4e2a\u53c2\u6570\u9700\u8981 set\uff0cset \u65b9\u6cd5\u5199\u5b8c\uff0c\u4ee3\u7801\u5df2\u7ecf\u5199\u4e86\u5341\u6765\u884c\u4e86\u3002<\/li><li>\u6267\u884c\u6d4b\u8bd5\uff1a\u8fd9\u4e00\u6b65\u6bd4\u8f83\u7b80\u5355\uff0c\u76f4\u63a5\u8c03\u7528\u88ab\u6d4b\u65b9\u6cd5\u5373\u53ef\u3002<\/li><li>\u7ed3\u679c\u9a8c\u8bc1\uff1a\u8fd9\u91cc\u9664\u4e86\u9a8c\u8bc1\u88ab\u6d4b\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u5916\uff0c\u8fd8\u9700\u8981\u9a8c\u8bc1\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u662f\u5426\u6b63\u786e\uff0c\u67d0\u5916\u90e8\u65b9\u6cd5\u88ab\u8c03\u7528\u8fc7n\u6b21\u6216\u672a\u8c03\u7528\u8fc7\u3002<\/li><li>\u5fc5\u8981\u7684\u6e05\u7406\uff1a\u5bf9\u6253\u6869\u8fdb\u884c\u6e05\u7406\uff0c\u5bf9\u6570\u636e\u5e93\u810f\u6570\u636e\u8fdb\u884c\u6e05\u7406\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u4e8c\u3001 \u75db\u70b9<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>2.1 \u91cd\u6784\u4ee3\u7801\u9700\u8981\u6539\u5199\u5927\u91cf\u5355\u5143\u6d4b\u8bd5\u7528\u4f8b<\/strong><\/p>\n\n\n\n<p>\u5bf9\u5916\u7684 Service \u63a5\u53e3\u5728\u4e0d\u53d8\u7684\u60c5\u51b5\u4e0b\uff0c\u5bf9\u5185\u90e8\u5b9e\u73b0\u8fdb\u884c\u91cd\u6784\uff0c\u8fd9\u65f6\u5019\u5934\u75db\u7684\u95ee\u9898\u6765\u4e86\uff0c\u5927\u91cf\u7684 Service \u5c42\u5355\u5143\u6d4b\u8bd5,biz \u5c42\u5355\u5143\u6d4b\u8bd5\u90fd\u8981\u91cd\u5199\uff1b\u6709\u65f6\u5019 Service \u8c03\u7528 biz \u5c42\u63a5\u53e3\u65f6\uff0c\u53c2\u6570\u4f20\u9519\u4e86\uff0c\u800c\u7531\u4e8e\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u65f6\u4e0d\u89c4\u8303\uff0c\u53c2\u6570\u5339\u914d\u4f7f\u7528\u4e86 anyxxx()\uff0c\u5bfc\u81f4\u53c2\u6570\u4f20\u9519\u7684 bug \u672a\u88ab\u53d1\u73b0\u3002<\/p>\n\n\n\n<p><strong>2.2 \u6d4b\u8bd5\u5e93\u6570\u636e\u968f\u610f\u4fee\u6539\u5bfc\u81f4\u7684\u5355\u5143\u6d4b\u8bd5\u4e0d\u7a33\u5b9a<\/strong><\/p>\n\n\n\n<p>DAO \u5c42\u5355\u5143\u6d4b\u8bd5\u76f4\u8fde\u6d4b\u8bd5\u5e93\uff0c\u7531\u4e8e\u6d4b\u8bd5\u5e93\u7684\u6570\u636e\u53ef\u4ee5\u88ab\u4efb\u610f\u4fee\u6539\uff0c\u4ece\u800c\u5bfc\u81f4\u6d4b\u8bd5\u4f9d\u8d56\u7684\u6570\u636e\u88ab\u66f4\u6539\uff0c\u5355\u5143\u6d4b\u8bd5\u4e0d\u901a\u8fc7\uff0c\u53e6\u5916\u5f00\u53d1\u5728\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u65f6\uff0c\u6ca1\u6709\u6e05\u7406\u610f\u8bc6\uff0c\u5bfc\u81f4\u6d4b\u8bd5\u5e93\u5927\u91cf\u5783\u573e\u6570\u636e\u3002<\/p>\n\n\n\n<p><strong>2.3 \u5355\u5143\u6d4b\u8bd5\u7ed3\u679c\u6821\u9a8c\u7f3a\u5931<\/strong><\/p>\n\n\n\n<p>\u4f8b\u5982\u4e00\u4e2a SaveItem() \u63a5\u53e3\uff0c\u6267\u884c\u5b8c\u6210\u540e\u9664\u4e86\u8981\u9a8c\u8bc1\u6267\u884c\u6210\u529f\u4ee5\u5916\uff0c\u8fd8\u5e94\u8be5\u9a8c\u8bc1\u843d\u5e93\u6570\u636e\u7684\u6b63\u786e\u6027\uff0c\u800c\u7f16\u5199\u8fd9\u90e8\u5206\u6d4b\u8bd5\u4ee3\u7801\u9700\u8981\u5927\u91cf\u7684\u4f7f\u7528\u539f\u751f jdbc \u63a5\u53e3\u67e5\u8be2 sql\uff0c\u5e76\u9010\u5b57\u6bb5\u9a8c\u8bc1\u6b63\u786e\u6027\uff0c\u4ee3\u7801\u7f16\u5199\u6548\u7387\u4f4e\u4e0b\u3002<\/p>\n\n\n\n<p><strong>\u4e09\u3001\u51e0\u4e2a\u5e38\u7528\u7684\u6d4b\u8bd5\u6846\u67b6\u7684\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>3.1 \u6570\u636e\u5c42\u5355\u5143\u6d4b\u8bd5\u6846\u67b6 DbUnit<\/strong><\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f18\u96c5\u7684\u6784\u9020 DB \u5c42\u7684\u521d\u59cb\u5316\u6570\u636e\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version='1.0' encoding='UTF-8'?>\n&lt;dataset>\n    &lt;employee employee_uid='1' \n          start_date='2001-11-01'           \n          first_name='Andrew' \n          ssn='xxx-xx-xxxx' \n          last_name='Glover' \/>\n&lt;\/dataset><\/pre>\n\n\n\n<p>\u5176\u4e2d employee \u662f\u8981\u6784\u9020\u6570\u636e\u7684\u8868\u540d\uff0c\u540e\u9762\u7684\u952e\u503c\u5bf9\u662f\u5217\u540d\u53ca\u5bf9\u5e94\u7684\u503c\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7b2c\u4e00\u884c\u5fc5\u987b\u5305\u542b\u5b8c\u6574\u7684\u5b57\u6bb5\u540d\uff0c\u5426\u5219\u52a0\u8f7d\u7684\u6570\u636e\u4e2d\u5168\u90e8\u4f1a\u7f3a\u5931\u67d0\u4e9b\u5b57\u6bb5\u3002<\/p>\n\n\n\n<p><strong>3.2 \u5d4c\u5165\u5f0f\u7684\u5185\u5b58\u6570\u636e\u5e93H2<\/strong><\/p>\n\n\n\n<p>\u975e\u5e38\u9002\u5408\u5728\u6d4b\u8bd5\u7a0b\u5e8f\u4e2d\u4f7f\u7528\uff0c\u7a0b\u5e8f\u5173\u95ed\u65f6\u81ea\u52a8\u6e05\u7406\u6570\u636e\uff0cH2 \u6570\u636e\u5e93\u7684\u8868\u7ed3\u6784\u521d\u59cb\u5316\u662f\u901a\u8fc7 <code>jdbc:initialize-database<\/code> \u6807\u7b7e\u5b9e\u73b0\u7684\uff0c\u5355\u5143\u6d4b\u8bd5\u4e2d\u4f7f\u7528 H2 \u6570\u636e\u5e93\u975e\u5e38\u7b80\u5355\uff0c\u4ec5\u9700\u4fee\u6539 jdbc \u8fde\u63a5\u5373\u53ef\u3002 \u5f15\u5165\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;dependency>\n     &lt;groupId>com.h2database&lt;\/groupId>\n     &lt;artifactId>h2&lt;\/artifactId>\n     &lt;version>1.4.191&lt;\/version>\n     &lt;scope>test&lt;\/scope>\n&lt;\/dependency><\/pre>\n\n\n\n<p>\u6570\u636e\u6e90\u8fde\u63a5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">spring.datasource.url=jdbc:h2:mem:test\nspring.datasource.driver-class-name=org.hsqldb.jdbcDriver\nspring.datasource.username=root\nspring.datasource.password=<\/pre>\n\n\n\n<p>schema \u521d\u59cb\u5316:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;jdbc:initialize-database data-source=\"dataSource\" ignore-failures=\"NONE\">\n        &lt;jdbc:script location=\"classpath:h2\/schema.sql\" encoding=\"UTF-8\"\/>\n    &lt;\/jdbc:initialize-database><\/pre>\n\n\n\n<p><strong>3.3 Spring \u5c0f\u6269\u5c55 springockito<\/strong><\/p>\n\n\n\n<p>\u5b83\u7b80\u5316\u4e86\u5728\u96c6\u6210\u6d4b\u8bd5\u7684\u76f8\u5173\u4e0a\u4e0b\u6587XML\u6587\u4ef6\u4e2d\u521b\u5efa mockito mocks \u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n    xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n    xmlns:mockito=\"http:\/\/www.mockito.org\/spring\/mockito\"\n    xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd\n    http:\/\/www.mockito.org\/spring\/mockito http:\/\/www.mockito.org\/spring\/mockito.xsd\">\n...\n    &lt;mockito:mock id=\"accountService\" class=\"org.kubek2k.account.DefaultAccountService\" \/>\n..\n&lt;\/beans><\/pre>\n\n\n\n<p><strong>3.4 spring \u5b98\u65b9\u6d4b\u8bd5\u6846\u67b6 spring-test<\/strong><\/p>\n\n\n\n<p>\u76ee\u524d\u4e3b\u6d41\u7684\u5f00\u53d1\u6846\u67b6\u90fd\u5728\u4f7f\u7528 spring \u6846\u67b6\u7ba1\u7406 bean\uff0c\u5728\u6d4b\u8bd5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u7528\u671f\u671b\u80fd\u591f\u4f7f\u7528 spring \u6846\u67b6\uff0cspring-test \u6846\u67b6\u5e2e\u52a9\u6211\u4eec\u89e3\u51b3 bean \u7684\u6ce8\u5165\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@ContextConfiguration(locations = \"\/test-context.xml\", \n                        loader = SpringockitoContextLoader.class) \npublic class CustomLoaderXmlApplicationContextTests {\n    \/\/ class body...\n}<\/pre>\n\n\n\n<p><code>\"\/test-context.xml\"<\/code> \u6307\u5b9a\u4e86\u6d4b\u8bd5\u7c7b\u8fd0\u884c\u9700\u8981\u52a0\u8f7d\u7684 spring \u914d\u7f6e\u6587\u4ef6\u8def\u5f84\uff0c <code>SpringockitoContextLoader<\/code>\u6307\u5b9a\u4e86\u52a0\u8f7d\u914d\u7f6e\u7684\u7c7b\uff0c\u8fd9\u4e24\u4e2a\u4e00\u8d77\u7528\u53ef\u4ee5\u652f\u6301\u5728\u4f7f\u7528 spring xml \u914d\u7f6e\u7684\u540c\u65f6\u53ef\u4ee5\u5c06 mockito \u751f\u6210\u7684 mock \u5bf9\u8c61 bean \u6ce8\u5165 spring \u4e0a\u4e0b\u6587\u4e2d\u3002<\/p>\n\n\n\n<p><strong>3.5 \u652f\u6301\u9759\u6001\u65b9\u6cd5 mock \u7684 mock \u6846\u67b6 powermock<\/strong><\/p>\n\n\n\n<p>\u652f\u6301\u9759\u6001\u65b9\u6cd5 mock\uff0c\u540c\u65f6\u517c\u5bb9 mockito\uff0cpowermock \u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RunWith(PowerMockRunner.class)\n@PrepareForTest( { YourClassWithEgStaticMethod.class })\npublic class YourTestCase {\n...\n}<\/pre>\n\n\n\n<p><strong>\u56db\u3001\u57fa\u4e8e springtest+ut+powermock \u7684\u6d4b\u8bd5\u6846\u67b6<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-1024x385.png\" alt=\"\" class=\"wp-image-6867\" width=\"583\" height=\"218\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-1024x385.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-300x113.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-768x288.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-1536x577.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-2048x769.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-830x312.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-230x86.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-350x131.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-32-480x180.png 480w\" sizes=\"(max-width: 583px) 100vw, 583px\" \/><\/figure><\/div>\n\n\n\n<p>\u6709\u8d5e\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff0c\u6570\u636e\u5e93\u5c42\u4f7f\u7528 h2 \u6570\u636e\u5e93\u4ee3\u66ff\u6d4b\u8bd5\u5e93\uff0c\u9694\u79bb\u5355\u5143\u6d4b\u8bd5\u6570\u636e\u4e0e\u6d4b\u8bd5\u5e93\u6570\u636e\uff0c\u5728\u5355\u5143\u6d4b\u8bd5\u7ed3\u675f\u540e\u81ea\u52a8\u6e05\u7406\u6570\u636e\uff0c\u907f\u514d\u6c61\u67d3\u6d4b\u8bd5\u5e93\u6570\u636e\u53ca\u88ab\u6d4b\u8bd5\u5e93\u6570\u636e\u5f71\u54cd\uff0c\u57fa\u4e8e <code>DbUnit<\/code> \u53ef\u4ee5\u901a\u8fc7 xml \u6784\u9020 DB \u5c42\u521d\u59cb\u5316\u6570\u636e\uff0c\u5b9e\u73b0\u6d4b\u8bd5\u4ee3\u7801\u4e0e\u6d4b\u8bd5\u6570\u636e\u5206\u79bb\uff0c\u4f9d\u8d56 <code>spring jdbc<\/code>\u7684\u521d\u59cb\u5316\u811a\u672c\u521d\u59cb\u5316 h2 \u6570\u636e\u5e93\u7684\u8868\u7ed3\u6784\u3002<\/p>\n\n\n\n<p><strong>4.1 \u6570\u636e\u51c6\u5907<\/strong><\/p>\n\n\n\n<p>\u5355\u6d4b\u4f9d\u8d56\u7684 Db \u6570\u636e\uff0c\u901a\u8fc7\u6dfb\u52a0\u6d4b\u8bd5\u65b9\u6cd5\u76d1\u542c\u5668\uff0c\u5728 Junit \u6267\u884c\u524d\u901a\u8fc7 DbUnit \u5de5\u5177\u7c7b\uff0c\u52a0\u8f7d\u521d\u59cb\u5316\u6587\u4ef6\uff0c\u5199\u5165 H2 \u6570\u636e\u5e93\uff1b\u5355\u6d4b\u7684\u5165\u53c2\uff0c\u901a\u8fc7 param.json \u6587\u4ef6\uff0c\u4ee5 json \u683c\u5f0f\u7f16\u5199\u5165\u53c2\u6570\u636e\uff0c\u5229\u7528\u5de5\u5177\u7c7b\u8bfb\u53d6\u6587\u4ef6\u5e76 json \u53cd\u5e8f\u5217\u5316\u4e3a\u76ee\u6807 Class \u5b9e\u4f8b\u3002 H2 \u6570\u636e\u5e93\u7684\u8868\u7ed3\u6784\uff0c\u5219\u662f\u901a\u8fc7\u4e0a\u6587\u63d0\u5230\u7684 <code>jdbc:initialize-database<\/code> \u521d\u59cb\u5316\u7684\uff0c\u5f00\u53d1\u540c\u5b66\u5fc5\u987b\u4fdd\u8bc1\u6b64 schema \u4e0e\u7ebf\u4e0a\u7ed3\u6784\u7684\u4e00\u81f4\u6027\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u5355\u6d4b\u5931\u8d25\u3002 \u6dfb\u52a0\u65b9\u6cd5\u76d1\u542c\u5668 <code>@TestExecutionListeners({JunitMethodListener.class})<\/code> \u8fd9\u662f\u81ea\u5b9a\u4e49\u7684\u76d1\u542c\u5668\uff0c\u5728\u6267\u884c\u524d\u540e\u6267\u884c\u81ea\u5b9a\u4e49\u903b\u8f91\uff0c\u5305\u62ec\u6570\u636e\u51c6\u5907\u3001\u9a8c\u8bc1\u548c\u6e05\u7406\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class JunitMethodListener extends AbstractTestExecutionListener {\n\n    @Override\n    public void beforeTestMethod(TestContext testContext) throws Exception {\n\n        Method jdkMethod = testContext.getTestMethod();\n        if (jdkMethod == null) {\n            return;\n        }\n\n        Object classInstance = testContext.getTestInstance();\n        if (!(classInstance instanceof JunitRunner)) {\n            return;\n        }\n\n        TestMethod testMethod = jdkMethod.getAnnotation(TestMethod.class);\n        if (testMethod == null) {\n            return;\n        }\n\n        JunitRunner runner = (JunitRunner) classInstance;\n        runner.init();\n        if (testMethod.enablePrepare()) {\n            TestRunnerTool.prepare(testMethod, runner);\n        }\n    }\n\n    @Override\n    public void afterTestMethod(TestContext testContext) throws Exception {\n        boolean hasException = (testContext.getTestException() != null) ? true : false;\n\n        Method jdkMethod = testContext.getTestMethod();\n        if (jdkMethod == null) {\n            return;\n        }\n\n        Object classInstance = testContext.getTestInstance();\n        if (!(classInstance instanceof JunitRunner)) {\n            return;\n        }\n\n        TestMethod testMethod = jdkMethod.getAnnotation(TestMethod.class);\n        if (testMethod == null) {\n            return;\n        }\n\n        JunitRunner runner = (JunitRunner) classInstance;\n        if (!hasException &amp;&amp; testMethod.enableCheck()) {\n            TestRunnerTool.check(testMethod, runner);\n        }\n\n        if (testMethod.enablePrepare()) {\n            \/\/\u6e05\u7406\u6570\u636e\n            TestRunnerTool.clean(testMethod, runner);\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801\u793a\u4f8b\uff0cenablePrepare \u58f0\u660e\u9700\u8981\u51c6\u5907\u6570\u636e\uff0cprepareDateConfig \u58f0\u660e\u6570\u636e\u51c6\u5907\u7684\u6587\u4ef6\u8def\u5f84\uff0cprepareDateType \u662f\u6570\u636e\u51c6\u5907\u7684\u7c7b\u578b\uff0cxml -> DB\uff0c\u5f53\u7136\u4e5f\u652f\u6301\u66f4\u591a\u7684\u6587\u4ef6\u7c7b\u578b\uff0c\u5982 csv\uff0cxls\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@TestMethod(\n            enablePrepare = true,\n            prepareDateType = PrepareDataType.XML2DB,\n            prepareDateConfig = {PREPARE_XML_FILE_USER}\n    )\n    @Test\n    public void test_updateUser(){\n        ... \u5177\u4f53\u4ee3\u7801\u7701\u7565\n    }<\/pre>\n\n\n\n<p><strong>4.2 \u6869\u4ee3\u7801\u76f8\u5173\u6846\u67b6<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4e86\u4f7f\u88ab\u6d4b\u4ee3\u7801\u80fd\u591f\u72ec\u7acb\u8fd0\u884c\u3001\u5e76\u63a7\u5236\u88ab\u6d4b\u4ee3\u7801\u7684\u6267\u884c\u8def\u5f84\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5916\u90e8\u4f9d\u8d56\uff08\u5305\u62ec<a rel=\"noreferrer noopener\" href=\"https:\/\/cloud.tencent.com\/product\/tdmq?from=10680\" target=\"_blank\">\u4e2d\u95f4\u4ef6<\/a>\u3001\u9759\u6001\u51fd\u6570\u3001\u5916\u90e8\u670d\u52a1\uff09\u8fdb\u884c mock\uff0cmock \u6846\u67b6\u4f9d\u8d56\u7684\u662f <code>PowerMock<\/code> \u53ca <code>mockito<\/code>\uff0c\u5229\u7528 <code>spring-test<\/code> \u96c6\u6210 <code>springockito<\/code> \u5c06 mock \u7684 bean \u6ce8\u5165\u5230 Spring \u4e0a\u4e0b\u6587\u4e2d\u3002 \u4f7f\u7528 PowerMock \u8fd0\u884c Junit \u5355\u5143\u6d4b\u8bd5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RunWith(PowerMockRunner.class)\n@PowerMockIgnore({ \"javax.management.*\", \"javax.net.ssl.*\"})<\/pre>\n\n\n\n<p>PowerMock \u96c6\u6210 Spring TestContext \u6846\u67b6<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)\n@ContextConfiguration(\n        loader = SpringockitoContextLoader.class,\n        locations = {\n                \"classpath:applicationContext-test.xml\"  })<\/pre>\n\n\n\n<p><strong>4.3 \u7ed3\u679c\u9a8c\u8bc1<\/strong><\/p>\n\n\n\n<p>\u7ed3\u679c\u9a8c\u8bc1\uff0c\u5305\u62ec\u4e24\u90e8\u5206\uff0c\u4e00\u4e2a\u662f\u88ab\u6d4b\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u8fd9\u4e2a\u9700\u8981\u7f16\u5199\u8005\u81ea\u884c\u9a8c\u8bc1\uff0c\u53e6\u4e00\u4e2a\u662f\u5199\u5165\u6570\u636e\u5e93\u7684\u503c\uff0c\u8fd9\u90e8\u5206\u662f\u901a\u8fc7\u5728\u65b9\u6cd5\u4e0a\u6dfb\u52a0\u6ce8\u89e3\uff0c\u544a\u8bc9\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u8981\u9a8c\u8bc1\u7684\u8bed\u53e5\uff0c\u6267\u884c\u9a8c\u8bc1\u8bed\u53e5\u5e76\u4e0e\u671f\u671b\u503c\u6bd4\u8f83\u3002<\/p>\n\n\n\n<p>\u5355\u5143\u6d4b\u8bd5\u65b9\u6cd5\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@TestMethod(\n            enablePrepare = true,\n            prepareDateType = PrepareDataType.XML2DB,\n            prepareDateConfig = { PREPARE_XML_FILE_USER},\n            enableCheck = true,\n            checkConfigFiles = {\"\/saveUserCheck.json\"}\n    )\n    @Test\n    public void test_updateUser() throws IOException {\n        UserParam param = MockUtil.fromFile(\n                \"\/param.json\",\n                UserParam.class);\n\n        ... \n    }<\/pre>\n\n\n\n<p>saveUserCheck.json \u6587\u4ef6\u5185\u5bb9\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    \"check.type\": \"DB_CHECK\",\n    \"check.desc\": \"\u68c0\u67e5 \u66f4\u65b0\u7ed3\u679c\u6b63\u786e\u6027\",\n    \"check.sql.query\": \"select status from user where user_id=1\",\n    \"check.expected.data\": [\n      {\n        \"status\": 1\n      }\n    ]\n  }<\/pre>\n\n\n\n<p><strong>4.4 \u4ee5\u4e0b\u662f\u5355\u5143\u6d4b\u8bd5\u57fa\u7c7b\u7684\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-33-1024x710.png\" alt=\"\" class=\"wp-image-6868\" width=\"547\" height=\"379\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-33-1024x710.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-33-300x208.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-33-768x532.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-33-830x575.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-33-230x159.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-33-350x243.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-33-480x333.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-33.png 1524w\" sizes=\"(max-width: 547px) 100vw, 547px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6982\u8ff0 \u5355\u5143\u6d4b\u8bd5\u662f\u6307\u5bf9\u8f6f\u4ef6\u4e2d\u7684\u6700\u5c0f\u53ef\u6d4b\u8bd5\u5355\u5143\u8fdb\u884c\u68c0\u67e5\u548c\u9a8c\u8bc1\u3002\u5355\u5143\u5728\u8d28\u91cf\u4fdd\u8bc1\u4e2d\u662f\u975e\u5e38\u91cd\u8981\u7684\u73af\u8282\uff0c\u6839\u636e\u6d4b\u8bd5\u91d1\u5b57\u5854 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,60],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6863"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6863"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6863\/revisions"}],"predecessor-version":[{"id":6869,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6863\/revisions\/6869"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}