{"id":6439,"date":"2022-07-17T10:27:49","date_gmt":"2022-07-17T02:27:49","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6439"},"modified":"2022-07-17T18:00:09","modified_gmt":"2022-07-17T10:00:09","slug":"junit-mock","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6439","title":{"rendered":"JUnit + Mock"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1ahttps:\/\/blog.csdn.net\/why_still_confused\/article\/details\/54865296<\/p>\n\n\n\n<p><strong>\u4e00\u3001JUnit\u5e38\u7528\u6ce8\u89e3\u4e0e\u65ad\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.@Test<\/strong><\/p>\n\n\n\n<p>@Test\u6ce8\u89e3\u7684public void\u65b9\u6cd5\u5c06\u4f1a\u88ab\u5f53\u505a\u6d4b\u8bd5\u7528\u4f8b<\/p>\n\n\n\n<p>JUnit\u6bcf\u6b21\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6d4b\u8bd5\u5b9e\u4f8b\uff0c\u7136\u540e\u8c03\u7528@Test\u6ce8\u89e3\u65b9\u6cd5\uff0c\u4efb\u4f55\u5f02\u5e38\u7684\u629b\u51fa\u90fd\u4f1a\u8ba4\u4e3a\u6d4b\u8bd5\u5931\u8d25<\/p>\n\n\n\n<p>@Test\u6ce8\u89e3\u63d0\u4f9b2\u4e2a\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul><li>I.\u201dexpected\u201d\uff0c\u5b9a\u4e49\u6d4b\u8bd5\u65b9\u6cd5\u5e94\u8be5\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u5982\u679c\u6d4b\u8bd5\u65b9\u6cd5\u6ca1\u6709\u629b\u51fa\u5f02\u5e38\u6216\u8005\u629b\u51fa\u4e86\u4e00\u4e2a\u4e0d\u540c\u7684\u5f02\u5e38\uff0c\u6d4b\u8bd5\u5931\u8d25<\/li><li>II.\u201dtimeout\u201d\uff0c\u5982\u679c\u6d4b\u8bd5\u8fd0\u884c\u65f6\u95f4\u957f\u4e8e\u8be5\u5b9a\u4e49\u65f6\u95f4\uff0c\u6d4b\u8bd5\u5931\u8d25\uff08\u5355\u4f4d\u4e3a\u6beb\u79d2\uff09<\/li><\/ul>\n\n\n\n<p><p>@Test(expected = IllegalArgumentException.class) \u8868\u793a\u9a8c\u8bc1\u8fd9\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u5c06\u629b\u51fa IllegalArgumentException \u5f02\u5e38\uff0c\u5982\u679c\u6ca1\u6709\u629b\u51fa\u7684\u8bdd\uff0c\u5219\u6d4b\u8bd5\u5931\u8d25\u3002<\/p><\/p>\n\n\n\n<p><strong>2.@Before<\/strong><\/p>\n\n\n\n<p>\u5f53\u7f16\u5199\u6d4b\u8bd5\u65b9\u6cd5\u65f6\uff0c\u7ecf\u5e38\u4f1a\u53d1\u73b0\u4e00\u4e9b\u65b9\u6cd5\u5728\u6267\u884c\u524d\u9700\u8981\u521b\u5efa\u76f8\u540c\u7684\u5bf9\u8c61<\/p>\n\n\n\n<p>\u4f7f\u7528@Before\u6ce8\u89e3\u4e00\u4e2apublic void \u65b9\u6cd5\u4f1a\u4f7f\u8be5\u65b9\u6cd5\u5728\u6240\u6709@Test\u6ce8\u89e3\u65b9\u6cd5\u88ab\u6267\u884c\u524d\u6267\u884c\uff08\u90a3\u4e48\u5c31\u53ef\u4ee5\u5728\u8be5\u65b9\u6cd5\u4e2d\u521b\u5efa\u76f8\u540c\u7684\u5bf9\u8c61\uff09\uff0c\u7236\u7c7b\u7684@Before\u6ce8\u89e3\u65b9\u6cd5\u4f1a\u5728\u5b50\u7c7b\u7684@Before\u6ce8\u89e3\u65b9\u6cd5\u6267\u884c\u524d\u6267\u884c<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>3.@After<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u5728@Before\u6ce8\u89e3\u65b9\u6cd5\u4e2d\u5206\u914d\u4e86\u989d\u5916\u7684\u8d44\u6e90\uff0c\u90a3\u4e48\u5728\u6d4b\u8bd5\u6267\u884c\u5b8c\u540e\uff0c\u9700\u8981\u91ca\u653e\u5206\u914d\u7684\u8d44\u6e90\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528@After\u6ce8\u89e3\u4e00\u4e2apublic void\u65b9\u6cd5\u4f1a\u4f7f\u8be5\u65b9\u6cd5\u5728\u6240\u6709@Test\u6ce8\u89e3\u65b9\u6cd5\u6267\u884c\u540e\u88ab\u6267\u884c\uff08\u5373\u4f7f\u5728@Before\u6ce8\u89e3\u65b9\u6cd5\u3001@Test\u6ce8\u89e3\u65b9\u6cd5\u4e2d\u629b\u51fa\u4e86\u5f02\u5e38\uff0c\u6240\u6709\u7684@After\u6ce8\u89e3\u65b9\u6cd5\u4f9d\u7136\u4f1a\u88ab\u6267\u884c\uff09\uff0c\u7236\u7c7b\u4e2d\u7684@After\u6ce8\u89e3\u65b9\u6cd5\u4f1a\u5728\u5b50\u7c7b@After\u6ce8\u89e3\u65b9\u6cd5\u6267\u884c\u540e\u88ab\u6267\u884c<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>4.@BeforeClass \u548c @AfterClass<\/strong><\/p>\n\n\n\n<p>@BeforeClass \u548c @AfterClass\u3002 @BeforeClass \u7684\u4f5c\u7528\u662f\uff0c\u5728\u8dd1\u4e00\u4e2a\u6d4b\u8bd5\u7c7b\u7684\u6240\u6709\u6d4b\u8bd5\u65b9\u6cd5\u4e4b\u524d\uff0c\u4f1a\u6267\u884c\u4e00\u6b21\u88ab @BeforeClass \u4fee\u9970\u7684\u65b9\u6cd5\uff0c\u6267\u884c\u5b8c\u6240\u6709\u6d4b\u8bd5\u65b9\u6cd5\u4e4b\u540e\uff0c\u4f1a\u6267\u884c\u4e00\u904d\u88ab @AfterClass \u4fee\u9970\u7684\u65b9\u6cd5\u3002\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765setup\u548crelease\u4e00\u4e9b\u516c\u5171\u7684\u8d44\u6e90\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u88ab\u8fd9\u4e24\u4e2aannotation\u4fee\u9970\u7684\u65b9\u6cd5\u5fc5\u987b\u662f\u9759\u6001\u7684\uff0c\u5e76\u4e14\u53ea\u6267\u884c\u4e00\u6b21\u3002<\/p>\n\n\n\n<p><strong>5.Assert<\/strong><\/p>\n\n\n\n<p>Assert\uff08\u65ad\u8a00\uff09\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u5341\u5206\u5e38\u7528\uff0c\u5305\u62ec\u6709<\/p>\n\n\n\n<ul><li>Assert.assertTrue();<\/li><li>Assert.assertEquals();<\/li><li>Assert.assertNotNull();<\/li><\/ul>\n\n\n\n<p>\u6ce8\uff1a<\/p>\n\n\n\n<ul><li>1.Double\u7c7b\u578b\u7684\u6570\u636e\u6bd4\u8f83\u65f6\u9700\u8981\u7528\u4ee5\u4e0b\u8bed\u53e5\uff0c\u5176\u4e2ddelta\u4e3a\u5141\u8bb8\u8bef\u5dee<\/li><\/ul>\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=\"\">Assert.assertEquals(expected, actual, delta);<\/pre>\n\n\n\n<ul><li>2.\u6574\u4f53\u65ad\u8a00\uff1a\u5f53\u9700\u8981\u5bf9\u5927\u91cf\u5b57\u6bb5\u8fdb\u6765\u65ad\u8a00\u65f6\uff0c\u53ef\u4ee5\u91c7\u7528\u201d\u6574\u4f53\u65ad\u8a00\u201d\u7684\u65b9\u6cd5\u3002<\/li><\/ul>\n\n\n\n<ul><li>3.\u5728\u5b9e\u9645\u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u5148\u4e0d\u5199\u65ad\u8a00\uff0c\u800c\u662f\u628a\u7ed3\u679c\u8f6c\u4e3aJSON\u5e76\u6253\u5370\u51fa\u6765\uff0c\u7ecf\u8fc7\u4eba\u5de5\u68c0\u9a8c\u65e0\u8bef\u540e\uff0c\u518d\u5c06\u6253\u5370\u7ed3\u679c\u4f5c\u4e3a\u9884\u671f\u7ed3\u679c\u52a0\u4e0a\u65ad\u8a00\uff0c\u8fd9\u53ef\u4ee5\u5927\u5927\u8282\u7701\u65f6\u95f4\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u4e8c\u3001Mock<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>1.Mock<\/p>\n\n\n\n<p>Mock\u901a\u5e38\u662f\u6307\uff0c\u5728\u6d4b\u8bd5\u4e00\u4e2a\u5bf9\u8c61A\u65f6\uff0c\u6211\u4eec\u6784\u9020\u4e00\u4e9b\u5047\u7684\u5bf9\u8c61\u6765\u6a21\u62df\u4e0eA\u4e4b\u95f4\u7684\u4ea4\u4e92\uff0c\u800c\u8fd9\u4e9bMock\u5bf9\u8c61\u7684\u884c\u4e3a\u662f\u6211\u4eec\u4e8b\u5148\u8bbe\u5b9a\u4e14\u7b26\u5408\u9884\u671f\u3002\u901a\u8fc7\u8fd9\u4e9bMock\u5bf9\u8c61\u6765\u6d4b\u8bd5A\u5728\u6b63\u5e38\u903b\u8f91\uff0c\u5f02\u5e38\u903b\u8f91\u6216\u538b\u529b\u60c5\u51b5\u4e0b\u5de5\u4f5c\u662f\u5426\u6b63\u5e38\u3002<\/p>\n\n\n\n<p>\u5f15\u5165Mock\u6700\u5927\u7684\u4f18\u52bf\u5728\u4e8e\uff1aMock\u7684\u884c\u4e3a\u56fa\u5b9a\uff0c\u5b83\u786e\u4fdd\u5f53\u4f60\u8bbf\u95ee\u8be5Mock\u7684\u67d0\u4e2a\u65b9\u6cd5\u65f6\u603b\u662f\u80fd\u591f\u83b7\u5f97\u4e00\u4e2a\u6ca1\u6709\u4efb\u4f55\u903b\u8f91\u7684\u76f4\u63a5\u5c31\u8fd4\u56de\u7684\u9884\u671f\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u5148\u6765\u770b\u770b\u4e0b\u9762\u8fd9\u4e2a\u793a\u4f8b\uff1a<\/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\/07\/\u56fe\u7247-176.png\" alt=\"\" class=\"wp-image-6445\" width=\"485\" height=\"152\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-176.png 800w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-176-300x94.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-176-768x240.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-176-230x72.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-176-350x109.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-176-480x150.png 480w\" sizes=\"(max-width: 485px) 100vw, 485px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\u5982\u679c\u6211\u4eec\u8981\u5bf9A\u8fdb\u884c\u6d4b\u8bd5\uff0c\u90a3\u4e48\u5c31\u8981\u5148\u628a\u6574\u4e2a\u4f9d\u8d56\u6811\u6784\u5efa\u51fa\u6765\uff0c\u4e5f\u5c31\u662fBCDE\u7684\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<p>\u4e00\u79cd\u66ff\u4ee3\u65b9\u6848\u5c31\u662f\u4f7f\u7528mocks<\/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\/07\/\u56fe\u7247-177.png\" alt=\"\" class=\"wp-image-6446\" width=\"413\" height=\"154\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-177.png 782w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-177-300x112.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-177-768x287.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-177-230x86.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-177-350x131.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-177-480x179.png 480w\" sizes=\"(max-width: 413px) 100vw, 413px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u56fe\u4e2d\u53ef\u4ee5\u6e05\u6670\u7684\u770b\u51fa<\/p>\n\n\n\n<p>mock\u5bf9\u8c61\u5c31\u662f\u5728\u8c03\u8bd5\u671f\u95f4\u7528\u6765\u4f5c\u4e3a\u771f\u5b9e\u5bf9\u8c61\u7684\u66ff\u4ee3\u54c1\u3002<\/p>\n\n\n\n<p>mock\u6d4b\u8bd5\u5c31\u662f\u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u5bf9\u90a3\u4e9b\u4e0d\u5bb9\u6613\u6784\u5efa\u7684\u5bf9\u8c61\u7528\u4e00\u4e2a\u865a\u62df\u5bf9\u8c61\u6765\u4ee3\u66ff\u6d4b\u8bd5\u7684\u65b9\u6cd5\u5c31\u53ebmock\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>Mock Object\u7684\u4f7f\u7528\u901a\u5e38\u4f1a\u5e26\u6765\u4ee5\u4e0b\u4e00\u4e9b\u597d\u5904\uff1a<\/p>\n\n\n\n<ul><li>\u9694\u7edd\u5176\u4ed6\u6a21\u5757\u51fa\u9519\u5f15\u8d77\u672c\u6a21\u5757\u7684\u6d4b\u8bd5\u9519\u8bef\u3002<\/li><li>\u9694\u7edd\u5176\u4ed6\u6a21\u5757\u7684\u5f00\u53d1\u72b6\u6001\uff0c\u53ea\u8981\u5b9a\u4e49\u597d\u63a5\u53e3\uff0c\u4e0d\u7528\u7ba1\u4ed6\u4eec\u5f00\u53d1\u6709\u6ca1\u6709\u5b8c\u6210\u3002<\/li><li>\u4e00\u4e9b\u901f\u5ea6\u8f83\u6162\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7528Mock Object\u4ee3\u66ff\uff0c\u5feb\u901f\u8fd4\u56de\u3002<\/li><\/ul>\n\n\n\n<p>\u5bf9\u4e8e<strong>\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u6d4b\u8bd5<\/strong>\uff0c\u4f7f\u7528Mock Object\u4f1a\u6709\u53e6\u5916\u4e24\u9879\u5f88\u91cd\u8981\u7684\u6536\u76ca\uff1a<\/p>\n\n\n\n<ul><li>\u901a\u8fc7Mock Object\u53ef\u4ee5\u5c06\u4e00\u4e9b\u5206\u5e03\u5f0f\u6d4b\u8bd5\u8f6c\u5316\u4e3a\u672c\u5730\u7684\u6d4b\u8bd5<\/li><li>\u5c06Mock\u7528\u4e8e\u538b\u529b\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u89e3\u51b3\u6d4b\u8bd5\u96c6\u7fa4\u65e0\u6cd5\u6a21\u62df\u7ebf\u4e0a\u96c6\u7fa4\u5927\u89c4\u6a21\u4e0b\u7684\u538b\u529b<\/li><\/ul>\n\n\n\n<p><strong>2.Mock\u7684\u5e94\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<p>\u5728\u4f7f\u7528Mock\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53d1\u73b0Mock\u662f\u6709\u4e00\u4e9b\u901a\u7528\u6027\u7684\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u5e94\u7528\u573a\u666f\uff0c\u662f\u975e\u5e38\u9002\u5408\u4f7f\u7528Mock\u7684\uff1a<\/p>\n\n\n\n<ul><li>\u771f\u5b9e\u5bf9\u8c61\u5177\u6709\u4e0d\u53ef\u786e\u5b9a\u7684\u884c\u4e3a(\u4ea7\u751f\u4e0d\u53ef\u9884\u6d4b\u7684\u7ed3\u679c\uff0c\u5982\u80a1\u7968\u7684\u884c\u60c5)<\/li><li>\u771f\u5b9e\u5bf9\u8c61\u5f88\u96be\u88ab\u521b\u5efa(\u6bd4\u5982\u5177\u4f53\u7684web\u5bb9\u5668)<\/li><li>\u771f\u5b9e\u5bf9\u8c61\u7684\u67d0\u4e9b\u884c\u4e3a\u5f88\u96be\u89e6\u53d1(\u6bd4\u5982\u7f51\u7edc\u9519\u8bef)<\/li><li>\u771f\u5b9e\u60c5\u51b5\u4ee4\u7a0b\u5e8f\u7684\u8fd0\u884c\u901f\u5ea6\u5f88\u6162<\/li><li>\u6d4b\u8bd5\u9700\u8981\u8be2\u95ee\u771f\u5b9e\u5bf9\u8c61\u5b83\u662f\u5982\u4f55\u88ab\u8c03\u7528\u7684(\u6bd4\u5982\u6d4b\u8bd5\u53ef\u80fd\u9700\u8981\u9a8c\u8bc1\u67d0\u4e2a\u56de\u8c03\u51fd\u6570\u662f\u5426\u88ab\u8c03\u7528\u4e86)<\/li><li>\u771f\u5b9e\u5bf9\u8c61\u5b9e\u9645\u4e0a\u5e76\u4e0d\u5b58\u5728(\u5f53\u9700\u8981\u548c\u5176\u4ed6\u5f00\u53d1\u5c0f\u7ec4\uff0c\u6216\u8005\u65b0\u7684\u786c\u4ef6\u7cfb\u7edf\u6253\u4ea4\u9053\u7684\u65f6\u5019\uff0c\u8fd9\u662f\u4e00\u4e2a\u666e\u904d\u7684\u95ee\u9898)<\/li><\/ul>\n\n\n\n<p>\u5f53\u7136\uff0c\u4e5f\u6709\u4e00\u4e9b\u4e0d\u5f97\u4e0dMock\u7684\u573a\u666f\uff1a<\/p>\n\n\n\n<ul><li>\u4e00\u4e9b\u6bd4\u8f83\u96be\u6784\u9020\u7684Object\uff1a\u8fd9\u7c7bObject\u901a\u5e38\u6709\u5f88\u591a\u4f9d\u8d56\uff0c\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u6784\u9020\u51fa\u8fd9\u6837\u7c7b\u901a\u5e38\u82b1\u8d39\u7684\u6210\u672c\u592a\u5927\u3002<\/li><li>\u6267\u884c\u64cd\u4f5c\u7684\u65f6\u95f4\u8f83\u957fObject\uff1a\u6709\u4e00\u4e9bObject\u7684\u64cd\u4f5c\u8d39\u65f6\uff0c\u800c\u88ab\u6d4b\u5bf9\u8c61\u4f9d\u8d56\u4e8e\u8fd9\u4e00\u4e2a\u64cd\u4f5c\u7684\u6267\u884c\u7ed3\u679c\uff0c\u4f8b\u5982\u5927\u6587\u4ef6\u5199\u64cd\u4f5c\uff0c\u6570\u636e\u7684\u66f4\u65b0\u7b49\u7b49\uff0c\u51fa\u4e8e\u6d4b\u8bd5\u7684\u9700\u6c42\uff0c\u901a\u5e38\u5c06\u8fd9\u7c7b\u64cd\u4f5c\u8fdb\u884cMock\u3002<\/li><li>\u5f02\u5e38\u903b\u8f91\uff1a\u4e00\u4e9b\u5f02\u5e38\u7684\u903b\u8f91\u5f80\u5f80\u5728\u6b63\u5e38\u6d4b\u8bd5\u4e2d\u662f\u5f88\u96be\u89e6\u53d1\u7684\uff0c\u901a\u8fc7Mock\u53ef\u4ee5\u4eba\u4e3a\u7684\u63a7\u5236\u89e6\u53d1\u5f02\u5e38\u903b\u8f91\u3002<\/li><\/ul>\n\n\n\n<p>\u5728\u4e00\u4e9b\u538b\u529b\u6d4b\u8bd5\u7684\u573a\u666f\u4e0b\uff0c\u4e5f\u4e0d\u5f97\u4e0d\u4f7f\u7528Mock\uff0c\u4f8b\u5982\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u6d4b\u8bd5\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u6d4b\u8bd5\u4e00\u4e9b\u5355\u70b9\uff08\u5982namenode\uff0cjobtracker\uff09\u5728\u538b\u529b\u573a\u666f\u4e0b\u7684\u5de5\u4f5c\u662f\u5426\u6b63\u5e38\u3002\u800c\u901a\u5e38\u6d4b\u8bd5\u96c6\u7fa4\u5728\u6b63\u5e38\u903b\u8f91\u4e0b\u65e0\u6cd5\u63d0\u4f9b\u8db3\u591f\u7684\u538b\u529b\uff08\u4e3b\u8981\u539f\u56e0\u662f\u53d7\u9650\u4e8e\u673a\u5668\u6570\u91cf\uff09\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u5e94\u7528Mock\u53bb\u6ee1\u8db3\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>JUnit5+Mockito\u3067\u5358\u4f53\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong><a href=\"https:\/\/qiita.com\/gitcho\/items\/5ce478ec8e29bbddc152#%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%82%B3%E3%83%BC%E3%83%89%E4%BD%9C%E6%88%90\"><\/a>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u4f5c\u6210<\/strong><\/p>\n\n\n\n<p>\u30c6\u30b9\u30c8\u5bfe\u8c61\u3068\u306a\u308b\u3001\u30b5\u30f3\u30d7\u30eb\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3002<br><code>MainService<\/code>\u30af\u30e9\u30b9\u304b\u3089<code>SubService<\/code>\u30af\u30e9\u30b9\u3092\u547c\u3073\u51fa\u3059\u60f3\u5b9a\u3068\u3057\u3066\u3044\u308b\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 MainService {\n    private SubService subService = new SubService();\n\n    public int getSum() {\n        int sum = 0;\n        for (int i : subService.getRandomIntegerList()) {\n            sum += i;\n        }\n        return sum;\n    }\n}\n<\/pre>\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 SubService {\n    public int[] getRandomIntegerList() {\n        return new int[]{8, 7, 2, 3, 6, 4, 5, 8, 4, 0};\n    }\n}\n<\/pre>\n\n\n\n<p><strong><a href=\"https:\/\/qiita.com\/gitcho\/items\/5ce478ec8e29bbddc152#%E3%83%86%E3%82%B9%E3%83%88%E3%82%B3%E3%83%BC%E3%83%89%E4%BD%9C%E6%88%90\"><\/a>\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u4f5c\u6210<\/strong><br><code>MainService<\/code>\u306e<code>getSum()<\/code>\u3092\u30c6\u30b9\u30c8\u3059\u308b\u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3059\u308b\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=\"\">import org.junit.jupiter.api.Assertions;\nimport org.junit.jupiter.api.Test;\nimport org.junit.jupiter.api.extension.ExtendWith;\nimport org.mockito.InjectMocks;\nimport org.mockito.Mock;\nimport org.mockito.Mockito;\nimport org.mockito.junit.jupiter.MockitoExtension;\n\n@ExtendWith(MockitoExtension.class)  \/\/ JUnit5\u3067Mockito\u4f7f\u3046\u306b\u306f\u5fc5\u8981\nclass MainServiceTest {\n\n    @Mock \/\/ \u30e2\u30c3\u30af\uff08\u30b9\u30bf\u30d6\uff09\u306b\u7f6e\u304d\u63db\u3048\u305f\u3044\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u5b9a\u7fa9\u3002\u3059\u3079\u3066\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u30e2\u30c3\u30af\u306b\u306a\u308b\n    \/\/@Spy \/\/ \u4e00\u90e8\u306e\u30e1\u30bd\u30c3\u30c9\u3060\u3051\u30e2\u30c3\u30af\u306b\u3057\u305f\u3044\u3068\u304d\u306f\u3053\u308c\u3092\u5b9a\u7fa9\n    private SubService subService;\n\n    @InjectMocks \/\/ @Mock\u3067\u30e2\u30c3\u30af\u306b\u3057\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u6ce8\u5165\u5148\u3068\u306a\u308b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u5b9a\u7fa9\n    private MainService mainService;\n\n    @Test\n    public void testGetSum() {\n        Mockito.when(this.subService.getRandomIntegerList()) \/\/ \u3053\u306e\u30e2\u30c3\u30af\u3092\u547c\u3073\u51fa\u3057\u305f\u3068\u304d\n                .thenReturn(new int[]{10, 20, 30, 40});  \/\/\u3053\u306e\u30c7\u30fc\u30bf\u3092\u8fd4\u3059\u3088\u3046\u306b\u30e2\u30c3\u30af\u3059\u308b\n        Assertions.assertEquals(this.mainService.getSum(), 100);\n    }\n}\n<\/pre>\n\n\n\n<p><strong><a href=\"https:\/\/qiita.com\/gitcho\/items\/5ce478ec8e29bbddc152#extendwith%E3%81%A3%E3%81%A6%E3%81%AA%E3%81%AB\"><\/a><code>@ExtendWith<\/code>\u3063\u3066\u306a\u306b\uff1f<\/strong><\/p>\n\n\n\n<p>JUnit4\u3067\u306f<code>@RunWith<\/code>\u3060\u3063\u305f\u3082\u306e\u3002JUnit5\u3067\u306f\u3053\u308c\u306b\u306a\u3063\u305f\u3002<br>\u307e\u305f\u3001Mockito\u3092\u4f7f\u3046\u3068\u304d\u306f\u3001Mockito\u304c\u5c02\u7528\u306eExtension(<code>MockitoExtension<\/code>)\u3092\u7528\u610f\u3057\u3066\u3044\u308b\u306e\u3067\u3053\u308c\u3092\u6307\u5b9a\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3002<br><\/p>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1ahttps:\/\/blog.csdn.net\/why_still_confused\/article\/det [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6439"}],"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=6439"}],"version-history":[{"count":9,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6439\/revisions"}],"predecessor-version":[{"id":6474,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6439\/revisions\/6474"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}