{"id":9943,"date":"2023-03-16T11:18:01","date_gmt":"2023-03-16T03:18:01","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9943"},"modified":"2023-03-16T12:59:41","modified_gmt":"2023-03-16T04:59:41","slug":"%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95mock%e4%b9%8bmockito%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9943","title":{"rendered":"\u5355\u5143\u6d4b\u8bd5Mock\u4e4bMockito\u4f7f\u7528\u6559\u7a0b"},"content":{"rendered":"\n<p><strong>\u4e00\u3001\u4ec0\u4e48\u662f<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=Mockito&amp;spm=1001.2101.3001.7020\" target=\"_blank\">Mockito<\/a><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Mockito\u662f\u4e00\u6b3e\u7528\u4e8ejava\u5f00\u53d1\u7684<a href=\"https:\/\/so.csdn.net\/so\/search?q=mock%E6%B5%8B%E8%AF%95&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noreferrer noopener\">mock\u6d4b\u8bd5<\/a>\u6846\u67b6\uff0c\u7528\u4e8e\u5feb\u901f\u521b\u5efa\u548c\u914d\u7f6emock\u5bf9\u8c61\u3002\u901a\u8fc7\u521b\u5efa\u5916\u90e8\u4f9d\u8d56\u7684 Mock \u5bf9\u8c61, \u7136\u540e\u5c06\u6b64 Mock \u5bf9\u8c61\u6ce8\u5165\u5230\u6d4b\u8bd5\u7c7b\u4e2d\uff0c\u7b80\u5316\u6709\u5916\u90e8\u4f9d\u8d56\u7684\u7c7b\u7684\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5bf9\u4e00\u4e2a\u53d1\u5956\u7cfb\u7edf\u670d\u52a1\u505a\u5355\u5143\u6d4b\u8bd5\uff0c\u670d\u52a1\u7684\u4f9d\u8d56\u5173\u7cfb\u5982\u4e0b\uff1a<\/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\/2023\/03\/\u56fe\u7247-85-1024x496.png\" alt=\"\" class=\"wp-image-9944\" width=\"468\" height=\"226\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-85-1024x496.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-85-300x145.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-85-768x372.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-85-830x402.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-85-230x111.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-85-350x170.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-85-480x233.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-85.png 1168w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/figure><\/div>\n\n\n\n<p>\u4e3a\u4e86\u5b8c\u6210RewardService\u7684\u6d4b\u8bd5\uff0c\u6211\u4eec\u9700\u8981\u53bb\u6784\u5efa\u771f\u5b9e\u7684DBService, CouponService, MemberService, \u6574\u4e2a\u8fc7\u7a0b\u5f88\u957f\uff0c\u6d89\u53ca\u591a\u4e2a\u670d\u52a1\uff0c\u6570\u636e\u6784\u9020\u5de5\u4f5c\u6bd4\u8f83\u7e41\u7410\u56f0\u96be\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u65f6\u5019\uff0c\u66f4\u7b80\u5355\u5feb\u6377\u7684\u65b9\u5f0f\u5c31\u662f\u901a\u8fc7mock\u7684\u65b9\u5f0f\u5c06\u4f9d\u8d56\u7684\u670d\u52a1\u4f7f\u7528\u865a\u62df\u7684\u5bf9\u8c61\uff08\u5373Mock\u7684\u5bf9\u8c61\uff09\u6765\u66ff\u4ee3\uff0c\u76f4\u63a5mock\u51fa\u6211\u4eec\u60f3\u8981\u7684\u6570\u636e\u6765\u8fdb\u884c\u6d4b\u8bd5\uff0c\u65b9\u4fbf\u5feb\u6377, \u4e0d\u9700\u8981\u5173\u6ce8\u90a3\u4e9b\u7684\u590d\u6742\u7684\u4f9d\u8d56\u4e86:<\/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\/2023\/03\/\u56fe\u7247-86-1024x508.png\" alt=\"\" class=\"wp-image-9945\" width=\"446\" height=\"221\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-86-1024x508.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-86-300x149.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-86-768x381.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-86-830x412.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-86-230x114.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-86-350x174.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-86-480x238.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-86.png 1152w\" sizes=\"(max-width: 446px) 100vw, 446px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u4e8c\u3001\u5728springboot\u4e2d\u4f7f\u7528Mockito<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>2.1 \u5f15\u5165maven\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<p>\u5728\u9879\u76eepom.xml\u4e2d\u5f15\u5165\u4f9d\u8d56<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=spring-boot&amp;spm=1001.2101.3001.7020\" target=\"_blank\">spring-boot<\/a>-starter-test\uff0c\u5185\u90e8\u5c31\u4f9d\u8d56\u4e86Mockito\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;dependency>\n    &lt;groupId>org.springframework.boot&lt;\/groupId>\n    &lt;artifactId>spring-boot-starter-test&lt;\/artifactId>\n&lt;\/dependency><\/pre>\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\/2023\/03\/\u56fe\u7247-87-801x1024.png\" alt=\"\" class=\"wp-image-9946\" width=\"401\" height=\"513\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-87-801x1024.png 801w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-87-235x300.png 235w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-87-768x982.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-87-830x1061.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-87-230x294.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-87-350x448.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-87-480x614.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-87.png 976w\" sizes=\"(max-width: 401px) 100vw, 401px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2.2 \u5173\u952e\u4ee3\u7801\u89e3\u6790<\/strong><\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\"><strong>1\u3001\u6dfb\u52a0\u6ce8\u89e3\uff0cmock\u670d\u52a1<\/strong><\/span><\/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=\"\">\/\/mockito\u6ce8\u89e3, \u7528\u4e8e\u542f\u7528Spring\u548cMockito\n@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)\npublic class RewardServiceTestMock{\n\n    \/\/\u901a\u8fc7@MockBean\u6ce8\u89e3\uff0c\u5c06TaskDubboService\u670d\u52a1\u7ed9mock\u6389\n    @MockBean\n    private RewardDubboService rewardDubboService;\n<\/pre>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">\u91cd\u8981\uff1a<\/span><\/strong>\u9996\u5148\uff0c\u5fc5\u987b\u52a0\u8fd9\u4e00\u884c\uff1a@TestExecutionListeners(listeners = MockitoTestExecutionListener.class) \uff0c\u7528\u4e8e\u542f\u7528Spring\u548cMockito\uff0c<span class=\"has-inline-color has-vivid-red-color\"><strong>\u4e4b\u524d\u7814\u7a76\u4e86\u534a\u5929\u7528\u4e0d\u8d77\uff0c\u62a5\u7a7a\u6307\u9488\uff0c\u5c31\u662f\u6ca1\u6709\u52a0\u8fd9\u4e00\u884c\u3002<\/strong><\/span><\/p>\n\n\n\n<p>\u7136\u540e\u4f7f\u7528\u6ce8\u89e3\uff1a@MockBean\uff0c\u628a\u8fd9\u4e2a\u670d\u52a1\u7ed9mock\u6389\u3002<\/p>\n\n\n\n<p><strong>2\u3001mock\u63a5\u53e3\u8fd4\u56de\u5185\u5bb9<\/strong><\/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=\"\">\/\/mock\u63a5\u53e3\u8fd4\u56de\u5185\u5bb9\nString msg1=\"[{\\\"class\\\":\\\"com.xxxx.RewardInfo\\\",\\\"rewardCount\\\":1,\\\"rewardName\\\":\\\"Mock\u8bf7\u6c421\u8fd4\u56de\u7684\u5956\u52b1\u5185\u5bb9\\\"}]\";\n<\/pre>\n\n\n\n<p><br>String msg1=xxxxxx, \u8fd9\u662f\u5b9a\u4e49\u4f60\u7684\u63a5\u53e3\u8fd4\u56de\u5185\u5bb9\u3002\u7531\u4e8e\u8fd9\u4e2a\u63a5\u53e3\u8fd4\u56de\u7684\u5185\u5bb9\u662f\u4e00\u4e2a\u5bf9\u8c61\u7c7b\u578b\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u4e0b\u9762\u8fd9\u53e5\u4ee3\u7801\u628aString\u5185\u5bb9\u8f6c\u6362\u6210\u6307\u5b9a\u7684\u5bf9\u8c61\u7c7b\u578b\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=\"\">List&lt;RewardInfo> mockResult1= JSONArray.parseArray(msg1,RewardInfo.class);<\/pre>\n\n\n\n<p><strong>3\u3001\u8bbe\u7f6emock\u89c4\u5219<\/strong><\/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=\"\">Mockito.when(rewardDubboService.reward(ParamContext)).thenReturn(mockResult1);<\/pre>\n\n\n\n<p>\u8868\u793a\u5f53\u8c03\u7528rewardService.reward(ParamContext)\u63a5\u53e3\u65f6\uff0c\u5c06\u63a5\u53e3\u7684\u8fd4\u56de\u5185\u5bb9mock\u4e3a\u4e4b\u524d\u5b9a\u4e49\u7684mockResult1\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p><strong>4\u3001 \u8c03\u7528\u63a5\u53e3<\/strong><\/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=\"\">List&lt;RewardInfo> result = rewardDubboService.reward(ParamContext);\nString resultStr = JSONObject.toJSONString(result);<\/pre>\n\n\n\n<p><strong>\u6574\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/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=\"\">\/\/mockito\u6ce8\u89e3, \u7528\u4e8e\u542f\u7528Spring\u548cMockito\n@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)\npublic class RewardServiceTestMock{\n\n    \/\/\u901a\u8fc7@MockBean\u6ce8\u89e3\uff0c\u5c06TaskDubboService\u670d\u52a1\u7ed9mock\u6389\n    @MockBean\n    private RewardDubboService rewardDubboService;\n\t\n\t\/\/mock\u63a5\u53e3\u8fd4\u56de\u5185\u5bb9\n\tString msg1=\"[{\\\"class\\\":\\\"com.xxxx.RewardInfo\\\",\\\"rewardCount\\\":1,\\\"rewardName\\\":\\\"Mock\u8bf7\u6c421\u8fd4\u56de\u7684\u5956\u52b1\u5185\u5bb9\\\"}]\";\n\t\n\t\/\/\u628aString\u5185\u5bb9\u8f6c\u6362\u6210\u6307\u5b9a\u7684\u5bf9\u8c61\u7c7b\u578b\n\tList&lt;RewardInfo> mockResult1= JSONArray.parseArray(msg1,RewardInfo.class);\n\t\n\t\/\/\u8bbe\u7f6emock\u89c4\u5219\n\tMockito.when(rewardDubboService.reward(ParamContext)).thenReturn(mockResult1);\n\t\n\t\/\/\u8c03\u7528\u63a5\u53e3\n\tList&lt;RewardInfo> result = rewardDubboService.reward(ParamContext);\n\tString resultStr = JSONObject.toJSONString(result);\n<\/pre>\n\n\n\n<p><strong>\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/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\/2023\/03\/\u56fe\u7247-88-1024x143.png\" alt=\"\" class=\"wp-image-9947\" width=\"584\" height=\"81\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-88-1024x143.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-88-300x42.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-88-768x107.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-88-1536x214.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-88-830x116.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-88-230x32.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-88-350x49.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-88-480x67.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-88.png 1552w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u4e09.\u8865\u5145\u8bf4\u660e<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\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=\"\">@SpringBootTest\n@RunWith(SpringRunner.class)\n@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)  \/\/step1\npublic class RuleRpcServiceTest extends KdAbstractJUnit4SpringContextTests {\n \n    @Mock\n    private OrderService orderService;\n \n\t\/\/step2\n    @InjectMocks\n    @Resource\n    private UserServiceImpl userService;\n \n    @Before\n    public void init() {\n        MockitoAnnotations.initMocks(this);\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=\"\">\uff081\uff09@RunWith \u6307\u5b9a\u8fd0\u884c\u73af\u5883\uff0c\u4f8b\uff1a\n   @RunWith(SpringRunner.class) Junit\u8fd0\u884cSpring\u7684\u6d4b\u8bd5\u73af\u5883\n   @RunWith(MockitoJUnitRunner.class) Junit\u8fd0\u884cMockito\u7684\u8fd0\u884c\u73af\u5883\uff0c\u4e0d\u4f1a\u52a0\u8f7dspringboot\u4e0a\u4e0b\u6587\n   @SpringBootTest \u52a0\u8f7dspringboot\u4e0a\u4e0b\u6587\u914d\u7f6e\n  \uff08\u6ce8\u610f\uff1a @RunWith(MockitoJUnitRunner.class) \u4e0d\u80fd\u548c @RunWith(SpringRunner.class) \u540c\u65f6\u4f7f\u7528\uff0c\u800c\u4e00\u822c\u90fd\u9700\u8981 @RunWith(SpringRunner.class) \u63d0\u4f9bspring\u5bb9\u5668\u73af\u5883\uff0c\u6240\u4ee5\u5f88\u5c11\u7528\u8fd9\u4e2a\u6ce8\u89e3\uff09\n\n\uff082\uff09@Mock \u7528\u4e8e\u6a21\u62df\u4e00\u4e2a\u670d\u52a1\uff0c\u4f8b\u5982\uff1a\n\n    \u6d4b\u8bd5\u7684\u65f6\u5019\u9700\u7528\u8c03\u7528B\u670d\u52a1\uff0c\u4f46\u662fB\u670d\u52a1\u6ca1\u5199 \u597d\uff0c\u8fd9\u662f\u53ef\u4ee5\u5148Mock\u4e00\u4e2aB\u670d\u52a1\n\n\uff083\uff09@InjectMocks \u6807\u6ce8\u670d\u52a1\u7684\u5b9e\u73b0\u7c7b\n\n    \u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5176\u4f59\u7528@Mock\u6ce8\u89e3\u521b\u5efa\u7684mock\u5c06\u88ab\u6ce8\u5165\u5230\u7528\u8be5\u5b9e\u4f8b\u4e2d\n\n\uff084\uff09@Before \u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u6267\u884c\u524d\uff0c\u6267\u884c\u4e00\u6b21\n\n\uff085\uff09@After \u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u6267\u884c\u5b8c\uff0c\u6267\u884c\u4e00\u6b21\n\n\uff086\uff09@Test \u6807\u6ce8\u6d4b\u8bd5\u65b9\u6cd5\n\n\uff087\uff09@MockBean \uff1a mock\u5bf9\u5e94\u5f15\u5165\u7684bean\n<\/pre>\n\n\n\n<p><strong>when\u2026then\u548cdo\u2026when\u7528\u6cd5<\/strong><\/p>\n\n\n\n<p>when\u2026then:<\/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\/\/thenReturn \u8fd4\u56de\u6307\u5b9a\u6570\u636e\nwhen(userService.findById(userId)).thenReturn(new User(userId, null));\n \n\/\/\u89e6\u53d1\u4e0a\u9762mock\u5b9a\u4e49\u7684thenReturn\nUser user1 = userService.findById(userId);\nSystem.out.println(user1);\n \n \n\/\/thenCallRealMethod \u8c03\u7528mock\u7c7b\u539f\u672c\u7684\u65b9\u6cd5\nwhen(userService.findById(userId)).thenCallRealMethod();\n<\/pre>\n\n\n\n<p>do\u2026when:<\/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=\"\">doReturn(new User(userId, null)).when(userService).findById(userId);\n \ndoCallRealMethod().when(userService).findById(userId);\n \ndoThrow(new RuntimeException(\"xxx\u4e0d\u5408\u6cd5\")).when(userService).findById(userId);<\/pre>\n\n\n\n<p><strong>\u6ce8\u5165\u5176\u5b83mock\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u4e00\u4e2amock\u5bf9\u8c61\u4e2d\u9700\u8981\u6ce8\u5165\u5176\u5b83mock\u5bf9\u8c61\uff0c\u6bd4\u5982\u8981mock UserService\uff0cUserService \u4f9d\u8d56\u4e8e\u53e6\u4e00\u4e2amock\u5bf9\u8c61orderService\uff0c\u8fd9\u65f6\u5c31\u4e0d\u80fd\u76f4\u63a5\u5bf9 UserSiervice \u4f7f\u7528 @Mock \u76f4\u63a5\u7684\u6ce8\u89e3\uff0c\u9700\u8981\uff1a<\/p>\n\n\n\n<ul><li>\u6807\u6ce8 @InjectMocks + @Autowired\u4e4b\u7c7b\u7684\u6ce8\u5165\u6ce8\u89e3<\/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=\"\">@SpringBootTest\n@RunWith(SpringRunner.class)\n@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)  \/\/step1\npublic class RuleRpcServiceTest extends KdAbstractJUnit4SpringContextTests {\n \n    @Mock\n    private OrderService orderService;\n \n\t\/\/step2\n    @InjectMocks\n    @Resource\n    private UserServiceImpl userService;\n \n    @Before\n    public void init() {\n        MockitoAnnotations.initMocks(this);\n    }\n    \n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4ec0\u4e48\u662fMockito Mockito\u662f\u4e00\u6b3e\u7528\u4e8ejava\u5f00\u53d1\u7684mock\u6d4b\u8bd5\u6846\u67b6\uff0c\u7528\u4e8e\u5feb\u901f\u521b\u5efa\u548c\u914d\u7f6emock\u5bf9 [&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\/9943"}],"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=9943"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9943\/revisions"}],"predecessor-version":[{"id":9954,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9943\/revisions\/9954"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}