{"id":7767,"date":"2022-10-21T11:33:06","date_gmt":"2022-10-21T03:33:06","guid":{"rendered":"http:\/\/123.57.164.21\/?p=7767"},"modified":"2023-09-27T17:36:58","modified_gmt":"2023-09-27T09:36:58","slug":"mockito%e7%9a%84%e4%bd%bf%e7%94%a8-injectmocks%e3%80%81spy%e3%80%81mock","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=7767","title":{"rendered":"Mockito\u7684\u4f7f\u7528 @InjectMocks\u3001@Spy\u3001@Mock"},"content":{"rendered":"\n<p>\u9879\u76ee\u4e2d\uff0c\u6709\u4e9b\u51fd\u6570\u9700\u8981\u5904\u7406\u67d0\u4e2a\u670d\u52a1\u7684\u8fd4\u56de\u7ed3\u679c\uff0c\u800c\u5728\u5bf9\u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u53c8\u4e0d\u80fd\u542f\u52a8\u90a3\u4e9b\u670d\u52a1\uff0c\u8fd9\u91cc\u5c31\u53ef\u4ee5\u5229\u7528Mockito\u5de5\u5177\uff0c\u5176\u4e2d\u6709\u5982\u4e0b\u4e09\u79cd\u6ce8\u89e3\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>@InjectMocks\uff1a\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u7b80\u5355\u7684\u8bf4\u662f\u8fd9\u4e2aMock\u53ef\u4ee5\u8c03\u7528\u771f\u5b9e\u4ee3\u7801\u7684\u65b9\u6cd5\uff0c\u5176\u4f59\u7528@Mock\uff08\u6216@Spy\uff09\u6ce8\u89e3\u521b\u5efa\u7684mock\u5c06\u88ab\u6ce8\u5165\u5230\u7528\u8be5\u5b9e\u4f8b\u4e2d\u3002<\/li><li>@Mock\uff1a\u5bf9\u51fd\u6570\u7684\u8c03\u7528\u5747\u6267\u884cmock\uff08\u5373\u865a\u5047\u51fd\u6570\uff09\uff0c\u4e0d\u6267\u884c\u771f\u6b63\u90e8\u5206\u3002<\/li><li>@Spy\uff1a\u5bf9\u51fd\u6570\u7684\u8c03\u7528\u5747\u6267\u884c\u771f\u6b63\u90e8\u5206\u3002<\/li><\/ul>\n\n\n\n<p>Mockito\u4e2d\u7684Mock\u548cSpy\u90fd\u53ef\u7528\u4e8e\u62e6\u622a\u90a3\u4e9b\u5c1a\u672a\u5b9e\u73b0\u6216\u4e0d\u671f\u671b\u88ab\u771f\u5b9e\u8c03\u7528\u7684\u5bf9\u8c61\u548c\u65b9\u6cd5\uff0c\u5e76\u4e3a\u5176\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u884c\u4e3a\u3002\u4e8c\u8005\u7684\u533a\u522b\u5728\u4e8eMock\u4e0d\u771f\u5b9e\u8c03\u7528\uff0cSpy\u4f1a\u771f\u5b9e\u8c03\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5177\u4f53\u7684\u5de5\u4f5c\u4e2d\u4f1a\u9047\u5230\u7684\u5177\u4f53\u6848\u4f8b\uff1a\n\u95ee\u98981\uff1a\n\u5b9e\u4f8b\u5bf9\u8c61\u7684\u6ce8\u5165\u5bf9\u8c61\u548c\u6ce8\u5165\u5bf9\u8c61\u542b\u6709\u76f8\u540c\u7684\u5c5e\u6027\u3002\u4e3e\u4f8b\u8bf4\u660e\uff1a\u5b9e\u4f8b\u5bf9\u8c61ClassA\u4e2d\u542b\u6709\u6ce8\u5165\u5bf9\u8c61ClassB\u3001ClassC\uff0c\u5b9e\u4f8b\u5bf9\u8c61ClassB\u4e2d\u4e5f\u542b\u6709ClassC\u3002\n\u95ee\u98982\uff1a\n\u5b9e\u4f8b\u5bf9\u8c61\u4e2d\u542b\u6709\u592a\u591a\u7684\u6ce8\u5165\u7c7b\uff0c\u5e76\u4e14\u5927\u90e8\u5206\u7684\u4ee3\u7801\u8d70\u771f\u5b9e\u8c03\u7528\u65b9\u5f0f\uff0c\u53ea\u60f3\u8981\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u90e8\u5206\u4ee3\u7801\u8d70Mock\u8c03\u7528\u7684\u65b9\u5f0f\u3002\n<\/pre>\n\n\n\n<p><strong>\u5bf9\u4e8e\u4e0a\u9762\u4e24\u4e2a\u95ee\u9898\u5176\u5b9e\u8981\u89e3\u51b3\u7684\u6839\u672c\u95ee\u9898\u5c31\u662f\uff1a\u5982\u4f55\u89e3\u51b3Mock\u6ce8\u5165\u51b2\u7a81\uff0c\u540c\u65f6\u6ce8\u5165\u4e24\u4e2a\u751a\u81f3\u591a\u4e2a\u5bf9\u8c61\u4e2d\uff1f<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\u4ecb\u7ecd\u4e00\u4e0bMockito\u7ed9\u6211\u4eec\u63d0\u4f9b\u7684\u4e00\u4e9b\u5de5\u5177\u7c7b\uff1a<a href=\"https:\/\/blog.csdn.net\/weixin_34413103\/article\/details\/86870036\">\u901a\u8fc7AopTestUtils\u5bf9\u5207\u9762\u5bf9\u8c61\u8fdb\u884cmock<\/a><\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u535a\u6587\u4e2d\u63d0\u4f9b\u7ed9\u4e86\u6211\u4eec\u5f88\u597d\u7684\u4e00\u4e2a\u601d\u8def\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5207\u9762\u65b9\u5f0f\u628a\u6bcf\u4e00\u4e2aMock\u5bf9\u8c61\u90fd\u653e\u5230\u5b9e\u4f8b\u5bf9\u8c61\u4e2d\u3002<br>\u4ee5\u4e0b\u662f\u6211\u7684\u89e3\u51b3\u65b9\u6848\uff1b<\/p>\n\n\n\n<ul><li>\u9996\u5148\uff1a\u5c06\u5b9e\u4f8b\u5bf9\u8c61\u6ce8\u5165\uff0c\u540c\u65f6\u4f7f\u7528Autowired\u548c@InjectMocks\u6ce8\u89e3<\/li><\/ul>\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\/10\/\u56fe\u7247-1024x678.png\" alt=\"\" class=\"wp-image-7768\" width=\"553\" height=\"366\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1024x678.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-300x199.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-768x509.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-830x550.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-230x152.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-350x232.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-480x318.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247.png 1262w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u518d\u8005\uff0c\u5c06Mock\u5bf9\u8c61\u901a\u8fc7\u53cd\u5c04\u5199\u5165\u5230\u5b9e\u4f8b\u5bf9\u8c61\u4e2d<\/li><\/ul>\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\/10\/\u56fe\u7247-1-1024x296.png\" alt=\"\" class=\"wp-image-7769\" width=\"677\" height=\"195\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1-1024x296.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1-300x87.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1-768x222.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1-1536x444.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1-830x240.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1-230x66.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1-350x101.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1-480x139.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-1.png 1834w\" sizes=\"(max-width: 677px) 100vw, 677px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u6700\u540e\uff0c\u89c4\u5b9a\u597d\u8fd4\u56de\u53c2\u6570\u5c31\u53ef\u4ee5\u8fdb\u884c\u6d41\u7a0b\u7684\u6d4b\u8bd5\u4e86\u3002<\/li><\/ul>\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\/10\/\u56fe\u7247-2-1024x205.png\" alt=\"\" class=\"wp-image-7770\" width=\"678\" height=\"135\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-2-1024x205.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-2-300x60.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-2-768x153.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-2-1536x307.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-2-830x166.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-2-230x46.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-2-350x70.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-2-480x96.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/10\/\u56fe\u7247-2.png 1742w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/figure><\/div>\n\n\n\n<p><strong>SpringBootTest \u6ce8\u5165 mock Mybatis mapper\u7684\u4f8b\u5b50\uff1a<\/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@AutoConfigureMockMvc\nclass ABCTest {\n\t\n    \/\/ \u6ce8\u5165\u771f\u5b9e\u7684ABCService\n\t@Autowired\n\t@InjectMocks\n\tABCService abcService;\n\t\n\t@Test\n\tvoid ABCtest_001() throws Exception {\n        \/\/ \u901a\u8fc7AopTestUtils\u83b7\u5f97Springboot\u6ce8\u5165\u7684ABCService\u5bf9\u8c61\n\t\tABCService service = AopTestUtils.getTargetObject(abcService);\n\t\tABCMapper abcMapperMock = mock(ABCMapper.class);\n        \/\/ \u901a\u8fc7ReflectionTestUtils \u53cd\u5c04\u5f80ABCService\u6ce8\u5165mock\u5bf9\u8c61\n\t\tReflectionTestUtils.setField(service, \"abcMapper\", abcMapperMock);\n\t\tdoThrow(new RuntimeException()).when(abcMapperMock).deleteByPrimaryKey(any());\n\t\t\n}<\/pre>\n\n\n\n<p><strong>\u5355\u5143\u6d4b\u8bd5\u4e4b@Mock\u4e0e@InjectMocks\u7684\u4f8b\u5b50<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>@Mock\u4e0e@InjectMocks\u4e00\u822c\u642d\u914d\u7ec4\u5408\u4f7f\u7528\uff0c\u662f\u5355\u5143\u6d4b\u8bd5\u5fc5\u4e0d\u53ef\u5c11\u7684\u6ce8\u89e3<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@Mock\uff1a\u9700\u8981\u6a21\u62df\u7684\u7c7b\uff0c\u6211\u4eec\u9700\u8981\u6a21\u62df\u54ea\u4e9b\u7c7b\uff0c\u5c31\u7528\u5b83\u4fee\u9970\u54ea\u4e9b\u7c7b\u7684\u53d8\u91cf\uff0c\u5e38\u7528\u4e8e\u7b2c\u4e09\u65b9\u670d\u52a1service\n@InjectMocks\uff1a\u8981\u6d4b\u8bd5\u7684\u7c7b\uff0c\u4f7f\u7528@Mock\u4fee\u9970\u7684\u5bf9\u8c61\uff0c\u5c31\u662f\u6211\u4eec\u6d4b\u8bd5\u54ea\u4e2a\u7c7b\uff0c\u5c31\u7528\u5b83\u4fee\u9970\u5bf9\u5e94\u7684\u53d8\u91cf\uff0c\u4f1a\u6574\u5408\u4f7f\u7528@Mock\u4fee\u9970\u7684\u5bf9\u8c61<\/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=\"\">@Service\npublic class ThirdService {\n\n    public Object getThirdUser(String userId) {\n        return new Object();\n    }\n}\n\n@Service\npublic class UserService {\n\n    @Autowired\n    private ThirdService thirdService;\n\n    public Object getUser(String userId) {\n        return thirdService.getThirdUser(userId);\n    }\n}\n\n\/\/\u6d4b\u8bd5UserService\n@SpringBootTest\nclass UserServiceTest {\n\n    \/\/\u9700\u8981\u6a21\u62df\u7684\u7c7b(\u56e0\u4e3aUserService\u4e2d\u4f7f\u7528\u4e86\u8fd9\u4e2a\u7c7b)\n    @Mock\n    private ThirdService thirdService;\n\n    \/\/\u8981\u6d4b\u8bd5\u7684\u7c7b\uff0c\u4f7f\u7528@Mock\u4fee\u9970\u7684\u5bf9\u8c61\n    \/\/\u8fd9\u65f6\u5019userService\u5bf9\u8c61\u4e2d\u6301\u6709\u7684thirdService\u53d8\u91cf\u5c31\u662f\u6a21\u62df\u7684\u5bf9\u8c61\u4e86\n    @InjectMocks\n    private UserService userService;\n\n    @Test\n    public void testGetUser() {\n        \/\/\u8bbe\u5b9a\u884c\u4e3a\u8fd4\u56de\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u5b9a\u6a21\u62df\u5bf9\u8c61\u7684\u884c\u4e3a\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4e0d\u8bbe\u5b9a\n        \/\/any\u4ee3\u8868\u4efb\u610f\u53c2\u6570\uff0c\u8fd4\u56dea\u5bf9\u8c61\n        Object a = new Object();\n        when(thirdService.getThirdUser(any())).thenReturn(a);\n\n        \/\/\u6267\u884c\u88ab\u6d4b\u8bd5\u7684\u65b9\u6cd5\uff0c\u8fd9\u65f6\u5019\u5185\u90e8\u8c03\u7528\u7684ThirdService\u5c31\u662f\u6a21\u62df\u5bf9\u8c61\u4e86\uff0c\u884c\u4e3a\u7ed3\u679c\u5c31\u662f\u4e0a\u9762\u8bbe\u7f6e\u7684\n        Object abc = userService.getUser(\"abc\");\n\n        \/\/\u9a8c\u8bc1\u7ed3\u679c\n        assertEquals(a,abc);\n    }\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u4e2d\uff0c\u6709\u4e9b\u51fd\u6570\u9700\u8981\u5904\u7406\u67d0\u4e2a\u670d\u52a1\u7684\u8fd4\u56de\u7ed3\u679c\uff0c\u800c\u5728\u5bf9\u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u53c8\u4e0d\u80fd\u542f\u52a8\u90a3\u4e9b\u670d\u52a1\uff0c\u8fd9\u91cc\u5c31\u53ef\u4ee5\u5229\u7528Moc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7767"}],"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=7767"}],"version-history":[{"count":6,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7767\/revisions"}],"predecessor-version":[{"id":12846,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7767\/revisions\/12846"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}