{"id":844,"date":"2021-01-04T19:12:02","date_gmt":"2021-01-04T11:12:02","guid":{"rendered":"https:\/\/blog.humh.cn\/?p=844"},"modified":"2021-01-04T19:19:35","modified_gmt":"2021-01-04T11:19:35","slug":"mockito%e4%b9%8bmock%e6%96%b9%e6%b3%95%e6%8a%9b%e5%87%ba%e5%8f%97%e6%a3%80%e6%9f%a5%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/blog.humh.cn\/?p=844","title":{"rendered":"Mockito\u4e4bmock\u65b9\u6cd5\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u57fa\u53cb\u5076\u7136\u95f4\u9047\u5230\u4e00\u4e2amock\u53d7\u68c0\u67e5\u5f02\u5e38\u7684\u95ee\u9898\uff0c\u8fd9\u91cc\u5b9e\u8df5\u540e\u603b\u7ed3\u4e00\u4e0b\u3002<\/p><\/blockquote>\n\n\n\n<p>\u8fd9\u91cc\u4f7f\u7528\u7684<strong>mockito<\/strong>\u8fdb\u884cUnitTest\u7684mock\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4>\u95ee\u9898\u63cf\u8ff0<\/h4>\n\n\n\n<p>\u5df2\u5728\u6d4b\u8bd5\u7c7b\u4e2d\u5b9a\u4e49\u5982\u4e0bmock bean<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> @MockBean(name = \"userProfileService\")\n private UserProfileService userProfileService;<\/pre>\n\n\n\n<p>UserProfileService \u4e2d\u5305\u542b\u6709\u8fd9\u6837\u4e00\u4e2a\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void test() {}<\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u7c7b\u4e2d\uff0c\u8fd9\u6837mock<\/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=\"\">@Test\npublic void testMock() {\n    try {\n        Mockito.doThrow(new IOException()).when(userProfileService).test();\n        userProfileService.test();\n    } catch (Exception e) {\n        System.out.println(\"eeeeeeeee\");\n    }\n}<\/pre>\n\n\n\n<p>\u5f53\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u4f1a\u6536\u5230\u8fd9\u6837\u7684\u5f02\u5e38\uff1a\u201cChecked exception is invalid for this method!\u201d<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22.png\"><img width=\"369\" height=\"73\" class=\"wp-image-845 lazyload\" title=\"Mockito\u4e4bmock\u65b9\u6cd5\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22.png\" alt=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22.png 369w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-300x59.png 300w\" sizes=\"(max-width: 369px) 100vw, 369px\"><\/a><\/figure><\/div>\n\n\n\n<h4>\u539f\u56e0\u5206\u6790\u53ca\u89e3\u51b3<\/h4>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053<strong>IOException<\/strong>\u5c5e\u4e8eJava Exception\u5f02\u5e38\u4e2d\u7684<strong>\u53d7\u68c0\u67e5\u5f02\u5e38<\/strong>\uff08Checked Exception\uff09\u3002\u53d7\u68c0\u67e5\u673a\u5236\u5728\u4e8e\uff0c\u5982\u679c\u4ee3\u7801\u5728\u8fd0\u884c\u4e2d\u9700\u8981\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38\uff0c\u5219\u5bf9\u5e94\u65b9\u6cd5\u7b7e\u540d\u540e\u5fc5\u987b\u58f0\u660e\u5f0f\u7684\u518d\u629b\u51fa\uff0c\u5373<code>throws IOException<\/code>\uff0c\u6216\u8005\u540c\u4ee3\u7801\u5757\u5185<code>catch<\/code>\u3002\u800cRuntimeException\u4e0d\u7528\u3002\u5982java.io\u5305\u4e0b\u7684\u5927\u591a\u6570\u65b9\u6cd5\u4e00\u6837\uff0c\u4f1a\u5728\u65b9\u6cd5\u4e0a\u6807\u6ce8<code>throws IOException<\/code>\uff0c\u8fd9\u6837\u505a\u7684\u76ee\u7684\u5c31\u662f\u8ba9\u8c03\u7528\u8005\u77e5\u9053\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u80fd\u5b58\u5728\u7f51\u7edc\u8fde\u63a5\u3001\u6587\u4ef6\u4e0d\u5b58\u5728\u7b49\u5bfc\u81f4\u7684IO\u5f02\u5e38\uff0c\u5728\u4ee3\u7801\u8fd0\u884c\u524d\u8981\u505a\u597d\u8fd9\u65b9\u9762\u903b\u8f91\u7684\u68c0\u67e5\uff01\u8fd9\u4e5f\u662fJava\u53d7\u68c0\u67e5\u5f02\u5e38\u8bbe\u8ba1\u521d\u7684\u76ee\u7684\uff0c\u5b83\u4f1a\u5728\u4ee3\u7801\u7f16\u8bd1\u671f\u5c31\u5f00\u59cb\u751f\u6548\uff01\uff08\u8bed\u8a00\u89c4\u5219\u4e0a\u5141\u8bb8catch\uff0c\u4f46\u603b\u4e0d\u80fd\u8fd9\u6837\u6ca1\u6709\u610f\u4e49\u7684\u8bbe\u8ba1\uff0c\u5f02\u5e38\u7684\u5904\u7406\u5e94\u8be5\u7531\u4e0b\u6e38\u8c03\u7528\u8005\u8fdb\u884c\uff09<strong>\u6362\u8a00\u4e4b\uff0c\u53d7\u68c0\u67e5\u673a\u5236\u5c31\u662f\uff0c\u9700\u8981\u8c03\u7528\u8005\u8fd0\u884c\u4e4b\u524d\u63d0\u524d\u68c0\u67e5\uff0c\u5e76\u505a\u597d\u51c6\u5907\uff0c\u4e0d\u4f1a\u8ba9\u8c03\u7528\u8005\u6ca1\u6709\u51c6\u5907\u7684\u60c5\u51b5\u4e0b\uff0c\u627f\u62c5\u5f02\u5e38\u98ce\u9669\uff01<\/strong><\/p>\n\n\n\n<p>\u540c\u65f6\u6ce8\u610f\u660e\u786ecatch\u5f02\u5e38\u7c7b\u578b\u4e3a\u53d7\u68c0\u67e5\u5f02\u5e38\u7684\u524d\u63d0\u662f\uff0ctry\u4e2d\u7684\u4ee3\u7801\u5fc5\u987b\u663e\u793a\u629b\u51fa\u5bf9\u5e94\u53d7\u68c0\u67e5\u5f02\u5e38\uff0c\u5426\u5219\u4e0d\u80fd\u660e\u786ecatch\u3002\u4e0d\u96be\u7406\u89e3\uff0c\u6bd5\u7adf\u4f5c\u7528\u4e8e\u7f16\u8bd1\u671f\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-1.png\"><img width=\"671\" height=\"200\" class=\"wp-image-846 lazyload\" title=\"Mockito\u4e4bmock\u65b9\u6cd5\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-1.png\" alt=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-1.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-1.png 671w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-1-300x89.png 300w\" sizes=\"(max-width: 671px) 100vw, 671px\"><\/a><\/figure><\/div>\n\n\n\n<p>\u56de\u5230\u201c\u95ee\u9898\u63cf\u8ff0\u201d\u4e2d\u7684\u4ee3\u7801\uff0c\u65b9\u6cd5\u672c\u8eab\u5e76\u6ca1\u6709throws\uff08\u88ab\u8c03\u7528\u65b9\u6cd5\u5185catch\u5728mock\u65f6\u4e5f\u6ca1\u7528\uff09\uff0c\u65e2\u7136\u6ca1\u6709\u544a\u77e5\u8c03\u7528\u8005\u8be5\u65b9\u6cd5\u53ef\u80fd\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38\uff0c\u5219\u8c03\u7528\u8005\u4e0d\u627f\u62c5\u8d23\u4efb\uff0c\u4e5f\u5c31\u4e0d\u5141\u8bb8\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u518d\u629b\u51fa\u6b64\u7c7b\u5f02\u5e38\uff0c\u629b\u51fa\u5219\u8fdd\u80cc\u673a\u5236\u3002\u6240\u4ee5mock\u8fd0\u884c\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u8fd9\u6837\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8981\u60f3\u67d0\u4e2a\u65b9\u6cd5\u5728mock\u4e2d\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38\uff0c\u5219\u5fc5\u987b\u63d0\u524d\u5728\u65b9\u6cd5\u4e0a<strong>throws<\/strong>\u58f0\u660e\u597d \u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void test() throws IOException{}<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-2.png\"><img width=\"654\" height=\"275\" class=\"wp-image-847 lazyload\" title=\"Mockito\u4e4bmock\u65b9\u6cd5\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-2.png\" alt=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-2.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-2.png 654w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-2-300x126.png 300w\" sizes=\"(max-width: 654px) 100vw, 654px\"><\/a><\/figure><\/div>\n\n\n\n<p>\u5982\u679cmock\u629b\u51faRuntimeException\uff0c\u5219\u4e0d\u9700\u8981\u4efb\u4f55\u5904\u7406\u3002<\/p>\n\n\n\n<h4>\u62d3\u5c55<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void test(){}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\npublic void testMock() {\n    try {\n        Mockito.doAnswer(invocation -&gt; {\n            throw new IOException();\n        }).when(userProfileService).test();\n        userProfileService.test();\n    } catch (Exception e) {\n        System.out.println(\"eeeeeeeee\");\n    }\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5\u4e0d\u53d8\uff0c\u5373\u6ca1\u6709\u58f0\u660e\u629b\u51fa\u4efb\u4f55\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\uff08doAnswer\u53c2\u6570\u7684\u5199\u6cd5\u4e3aAnswer\u63a5\u53e3\u533f\u540d\u5185\u90e8\u7c7b\u5b9e\u73b0\u7684lambda\u5199\u6cd5\uff09\u4ecd\u53ef\u4ee5\u5b8c\u6210\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38\u7684\u5de5\u4f5c\uff0c\u7ed3\u679c\u770b\u8d77\u6765\u548c\u201c\u539f\u56e0\u5206\u6790\u4e0e\u89e3\u51b3\u201d\u4e2d\u7684\u7ed3\u679c\u4e00\u6837\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-3.png\"><img width=\"599\" height=\"329\" class=\"wp-image-850 lazyload\" title=\"Mockito\u4e4bmock\u65b9\u6cd5\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-3.png\" alt=\"https:\/\/file.blog.humh.cn\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-3.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-3.png 599w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/01\/d2b5ca33bd970f64a6301fa75ae2eb22-3-300x165.png 300w\" sizes=\"(max-width: 599px) 100vw, 599px\"><\/a><\/figure><\/div>\n\n\n\n<p>\u4f46\u9700\u8981\u6ce8\u610f\uff01\u8fd9\u4ec5\u4ec5\u662f\u7ed3\u679c\u4e00\u6837\uff0c\u4ed6\u4eec\u7684\u5904\u7406\u8fc7\u7a0b\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u533a\u522b\u5728\u4e8edoThrow\u548cdoAnswer\u4e24\u4e2a\u65b9\u6cd5\u672c\u8eab\u7684\u533a\u522b\u3002<\/p>\n\n\n\n<ul><li><strong>doThrow<\/strong>\uff1a\u57fa\u672c\u4e0a\u7528\u4e8e\u5728\u6a21\u62df\u5bf9\u8c61\u5185\u8c03\u7528\u65b9\u6cd5\u65f6\u8981\u5f15\u53d1\u5f02\u5e38\u7684\u60c5\u51b5\uff0c\u76f8\u5f53\u4e8e\u5f02\u5e38\u629b\u51fa\u7684\u4f4d\u7f6e\u6e90\u5934\u662f\u5728\u65b9\u6cd5\u5185\u90e8\uff0c\u597d\u6bd4\u65b9\u6cd5\u5185\u90e8<code>throw new IOException<\/code>\u3002<\/li><li><strong>doAnswer<\/strong>\uff1a\u5c06\u65b9\u6cd5\u5165\u53c2\uff0c\u62ff\u6765\u53bb\u6267\u884canswer() \u65b9\u6cd5\u5185\u90e8\u7684\u903b\u8f91\uff0c\u7136\u540e\u5c06answer\u7684\u6267\u884c\u7ed3\u679c\u4f5c\u4e3amock\u7684\u76ee\u6807\u65b9\u6cd5\u201c\u8f93\u51fa\u201d\uff08\u8fd4\u56de\u503c\u6216\u629b\u5f02\u5e38\uff09\uff0c\u4e5f\u5c31\u662f\u76f8\u5f53\u4e8e\u6267\u884c\u5b8c\u76ee\u6807\u65b9\u6cd5\uff0c\u7136\u540e\u6267\u884c\u5c06answer() \u7684\u8f93\u51fa\u66ff\u4ee3\u76ee\u6807\u65b9\u6cd5\u7684\u8f93\u51fa\uff0c\u5e76\u4e0d\u662f\u5728\u76ee\u6807\u65b9\u6cd5\u5185\u5b8c\u6210\u3002<\/li><\/ul>\n\n\n\n<p>\u6240\u4ee5\u4e0a\u9762doAnswer\u8fd9\u79cd\u65b9\u5f0f\u5c31\u76f8\u5f53\u4e8euserProfileService.test() \u65b9\u6cd5\u4e0d\u7528\u58f0\u660e\u629b\u5f02\u5e38\uff0c\u7136\u540e\u6d4b\u8bd5\u7c7b\u4e2d\uff0c<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\npublic void testMock() {\n    try {\n        userProfileService.test();\n        throw new IOException();\n    } catch (Exception e) {\n        System.out.println(\"eeeeeeeee\");\n    }\n}<\/pre>\n\n\n\n<p>\u6240\u4ee5\u5e76\u6ca1\u6709\u8fdd\u80ccJava\u53d7\u68c0\u67e5\u5f02\u5e38\u7684\u673a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u53cb\u5076\u7136\u95f4\u9047\u5230\u4e00\u4e2amock\u53d7\u68c0\u67e5\u5f02\u5e38\u7684\u95ee\u9898\uff0c\u8fd9\u91cc\u5b9e\u8df5\u540e\u603b\u7ed3\u4e00\u4e0b\u3002 \u8fd9\u91cc\u4f7f\u7528\u7684mockito\u8fdb\u884cUnitTest [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":855,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[35,111,93],"_links":{"self":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/844"}],"collection":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=844"}],"version-history":[{"count":4,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/844\/revisions"}],"predecessor-version":[{"id":854,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/844\/revisions\/854"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/media\/855"}],"wp:attachment":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}