{"id":6586,"date":"2022-07-22T17:29:52","date_gmt":"2022-07-22T09:29:52","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6586"},"modified":"2022-10-21T11:18:36","modified_gmt":"2022-10-21T03:18:36","slug":"mockito-%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6586","title":{"rendered":"Mockito \u5b66\u4e60\u7b14\u8bb0(\u63a8\u8350)"},"content":{"rendered":"\n<p><strong>\u524d\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u56e0\u4e3a\u5355\u5143\u6d4b\u8bd5\u7684\u9700\u8981\uff0c\u7ecf\u5e38\u4f1a\u9700\u8981mock\u6389\u4e00\u4e9b\u5916\u90e8\u5f15\u7528\u6216\u8005\u6570\u636e\u5e93\u63a5\u5165\u7b49\u5185\u5bb9\u3002\u63a5\u89e6\u8fc7JMockit\u3001Mockito\u3001PowerMock\u7b49\u5de5\u5177\uff0c\u4f46\u6700\u7ec8\u8fd8\u662f\u9009\u62e9\u4e86Mockito\u3002<\/p>\n\n\n\n<p>\u76f8\u8f83\u4e4b\u4e0b\u3002JMockit\u7ecf\u5e38\u88ab\u8ba4\u4e3a\u662f\u6bd4\u8f83\u5168\u9762\u7684\uff0c\u9664\u4e86\u5e38\u89c1mock\u6389\u7c7b\u66b4\u9732\u7684\u65b9\u6cd5\uff0c\u7c7b\u5185\u7684\u79c1\u6709\u65b9\u6cd5\uff0c\u6784\u9020\u65b9\u6cd5\uff0c\u9759\u6001\u65b9\u6cd5\uff0c\u751a\u81f3\u7528final\u4fee\u9970\u7684\u65b9\u6cd5\u90fd\u80fd\u5f88\u597d\u7684mock\u6389\u3002<\/p>\n\n\n\n<p>\u800cMockito\u76f8\u5bf9\u4e86\u5176\u4ed6\u4e24\u79cd\uff0c\u5176\u8bed\u6cd5\u5341\u5206\u7684\u6613\u4e8e\u7406\u89e3\uff0c\u53ef\u8bfb\u6027\u5f88\u597d\u3002\u8fd9\u4e5f\u662f\u6211\u559c\u6b22Mockito\u7684\u539f\u56e0\uff0c\u800cPowerMock\u5219\u7c7b\u4f3c\u4e8eMockito\u7684\u8d85\u96c6\uff0c\u4f46Maven\u5f15\u5165PowerMock\u7684\u65f6\u5019\uff0c\u4e5f\u5c31\u81ea\u52a8\u5f15\u5165\u4e86Mockito\u3002<\/p>\n\n\n\n<p>Mockito\u5e38\u88ab\u4eba\u8ba4\u4e3a\u529f\u80fd\u4e0d\u591f\u5f3a\u5927\uff0c\u5bf9\u4e8e\u9759\u6001\u65b9\u6cd5\uff0c\u6784\u9020\u65b9\u6cd5\u7b49\u5185\u5bb9\uff0c\u9700\u8981\u501f\u52a9PowerMock\u624d\u80fd\u6267\u884c\u3002\u4f46\u90a3\u5df2\u7ecf\u662f\u51e0\u5e74\u524d\u7684\u4e8b\u60c5\u4e86\u30022016\u5e74\u63a8\u51fa\u4e86Mockito2.x\uff0c\u5df2\u7ecf\u89e3\u51b3\u4e86\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n\n\n\n<p>Mockito\u7684\u6e90\u7801\u4e2d\u6ce8\u91ca\u5341\u5206\u5168\u9762\u3002\u5176org.mockito.Mockito\u7c7b\u4e2d\u6ce8\u91ca\u4e86\u5927\u91cf\u76f8\u5173\u5185\u5bb9\uff0c\u4ed4\u7ec6\u9605\u8bfb\u5bf9Mockito\u7684\u4f7f\u7528\u5f88\u6709\u5e2e\u52a9\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e2a\u4eba\u5bf9Mockito\u5b66\u4e60\u7684\u4e00\u4e9b\u7b80\u6613\u603b\u7ed3\uff0c\u4e5f\u662f\u7b80\u5355\u8bb0\u5f55\u4ee5\u4f9b\u540e\u7eed\u6709\u9700\u67e5\u9605\u3002<\/p>\n\n\n\n<p><strong>1\u3001mock\u516c\u6709\u65b9\u6cd5<\/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=\"\">public class AppleTest {\n\n\t@Test\n\tpublic void test_mock_public_method() {\n\n\t\tApple apple = mock(Apple.class);\n\t\twhen(apple.getColor()).thenReturn(\"green\");\n\t\tAssertions.assertEquals(\"green\", apple.getColor());\n\n\t}\n}\n\nclass Apple {\n\n\tpublic String getColor() {\n\t\treturn \"red\";\n\t}\n\n}<\/pre>\n\n\n\n<p>\u8bed\u6cd5\u6bd4\u8f83\u7b80\u5355\uff0c\u521b\u5efamock\u5bf9\u8c61\uff0c\u7136\u540e\u7c7b\u4f3cwhen\u2026thenReturn\u2026\u8bed\u6cd5\u4fee\u6539\u65b9\u6cd5\u7684\u8fd4\u56de\uff0c\u4e5f\u53ef\u4f7f\u7528thenThrow\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<p><strong>2\u3001mock\u9759\u6001\u65b9\u6cd5<\/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=\"\">public class AppleTest {\n\n\t@Test\n\tpublic void test_mock_static_method() {\n\n\t\ttry (MockedStatic&lt;Apple> mockedStatic = Mockito.mockStatic(Apple.class)) {\n\n\t\t\twhen(Apple.getColor()).thenReturn(\"green\");\n\n\t\t\tAssertions.assertEquals(\"green\", Apple.getColor());\n\n\t\t}\n\n\t}\n\n}\n\nclass Apple {\n\n\tpublic static String getColor() {\n\n\t\treturn \"red\";\n\n\t}\n\n}<\/pre>\n\n\n\n<p>\u5b98\u65b9\u5efa\u8bae\u662f\u901a\u8fc7try-resouce\u8bed\u53e5\u5305\u88f9\u5185\u5bb9\u3002\u56e0\u4e3a\u91c7\u7528\u7684\u662fThreadLocal\u4fdd\u5b58\u5bf9\u9759\u6001\u65b9\u6cd5\u7684\u63a7\u5236\uff0ctry-resouce\u80fd\u4fdd\u8bc1mock\u4f7f\u7528\u7ed3\u675f\u540e\u8c03\u7528MockStatic.close()\u6e05\u9664\u3002\u4e5f\u53ef\u8c03\u7528Mockito.framework().clearInlineMocks(); \u4e00\u6b21\u6027\u6e05\u9664\u5168\u90e8Mock\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p><strong>3\u3001Mock\u6784\u9020\u65b9\u6cd5<\/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=\"\">public class AppleTest {\n\n\t@Test\n\tpublic void test_mock_construction_method() {\n\n\t\ttry (MockedConstruction&lt;Banana> mockConstruction\n\n\t\t\t\t= mockConstructionWithAnswer(Banana.class, invocation -> \"green\")) {\n\n\t\t\tApple apple = new Apple();\n\n\t\t\tAssertions.assertEquals(\"green\", apple.getColor());\n\n\t\t}\n\n\t}\n\n}\n\nclass Apple {\n\n\tpublic String getColor() {\n\n\t\treturn new Banana().getColor();\n\n\t}\n\n}\n\nclass Banana {\n\n\tpublic String getColor() {\n\n\t\treturn \"yellow\";\n\n\t}\n\n}<\/pre>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">\u91cd\u8981\uff1a\u666e\u901a\u7684\u4f9d\u8d56\u6ce8\u5165\uff0c\u90fd\u662f\u6bd4\u8f83\u597dmock\u7684\u3002\u4f46\u7f16\u5199\u7684\u4ee3\u7801\u91cc\u82e5\u662fnew\u4e86\u5176\u4ed6\u5bf9\u8c61\uff0c\u5c31\u4f1a\u6bd4\u8f83\u96bemock\uff0cmock\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5982\u679cmock\u6784\u9020\u65b9\u6cd5\u7684\u5bf9\u8c61\u884c\u4e3a\u6bd4\u8f83\u590d\u6742\u7684\u8bdd\uff0c\u4e5f\u53ef\u4ee5\u5148\u63d0\u524d\u6784\u9020mock\u5bf9\u8c61\u6765\u5904\u7406\u3002<\/span><\/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=\"\">Banana mockBanana = mock(Banana.class);\n\nwhen(mockBanana.getColor()).thenReturn(\"green\");\n\ntry(MockedConstruction&lt;Banana>mockConstruction\n\n            = mockConstructionWithAnswer(Banana.class,\n\n        invocation ->invocation.getMethod().invoke(mockBanana,invocation.getArguments()))){\n\n    Apple apple = new Apple();\n\n    Assertions.assertEquals(\"green\",apple.getColor());\n\n}<\/pre>\n\n\n\n<p>\u53e6\u4e00\u4e2amock\u6784\u9020\u51fd\u6570\u7684\u4f8b\u5b50<\/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=\"\">final Foo foo = mock(Foo.class);\n    MockedConstruction&lt;Foo> mockedConstruction = mockConstructionWithAnswer(Foo.class, new Answer() {\n        @Override\n        public Object answer(InvocationOnMock invocation) throws Throwable {\n            if (invocation.getMethod().equals(Foo.class.getMethod(\"getFooName\"))) {\n                return \"mocked value\";\n            }\n            return foo;\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=\"\">final Foo foo = mock(Foo.class);\n    MockedConstruction&lt;Foo> mockedConstruction = mockConstructionWithAnswer(Foo.class, new Answer() {\n        @Override\n        public Object answer(InvocationOnMock invocation) throws Throwable {\n            \/\/ \u5f53mock\u5bf9\u8c61\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c\u4f1a\u8fdb\u5165\u5230\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u9762\u3002\u901a\u8fc7\u5224\u65ad\u65b9\u6cd5\u540d\u5b57\uff0c\u8fd4\u56demock\u7684value\n            if (invocation.getMethod().getName().equals(\"getFooName\"))) {\n                return \"mocked value\";\n            }\n            return foo;\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=\"\">\/\/ mock\u5f53\u8c03\u7528\u5bf9\u8c61\u67d0\u4e2a\u65b9\u6cd5\u65f6\u51fa\u5f02\u5e38\nfinal Foo foo = mock(Foo.class);\n    MockedConstruction&lt;Foo> mockedConstruction = mockConstructionWithAnswer(Foo.class, new Answer() {\n        @Override\n        public Object answer(InvocationOnMock invocation) throws Throwable {\n            \/\/ \u5f53mock\u5bf9\u8c61\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c\u4f1a\u8fdb\u5165\u5230\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u9762\u3002\u901a\u8fc7\u5224\u65ad\u65b9\u6cd5\u540d\u5b57\uff0c\u8fd4\u56demock\u7684value\n            if (invocation.getMethod().getName().equals(\"getFooName\"))) {\n                throw new java.lang.RuntimeException();\n            }\n            return foo;\n        }\n    });<\/pre>\n\n\n\n<p><strong>4\u3001Mock\u79c1\u6709\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5176\u5b9e\u662f\u4e0d\u652f\u6301\u7684\u3002\u622a\u6b62\u5f53\u524d\u6700\u65b0\u76843.11.1\u7248\u672c\u3002Mockito\u672c\u8eab\u5e76\u4e0d\u652f\u6301mock\u79c1\u6709\u65b9\u6cd5\u3002\u5176\u5b98\u65b9\u662f\u6709\u8bf4\u660e\u539f\u56e0\u7684\uff0c\u5176\u6700\u540e\u4e00\u70b9\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=\"\">    Finally... Mocking private methods is a hint that there is something wrong with Object Oriented understanding. In OO you want objects (or roles) to collaborate, not methods. Forget about pascal &amp; procedural code.Think in objects.\n    https:\/\/github.com\/mockito\/mockito\/wiki\/Mockito-And-Private-Methods<\/pre>\n\n\n\n<p>\u8fd9\u4e00\u70b9\u8fd8\u662f\u6bd4\u8f83\u8ba4\u540c\u7684\uff0c\u5982\u679c\u9700\u8981mock\u79c1\u6709\u65b9\u6cd5\uff0c\u90a3\u5927\u6982\u7387\u7a0b\u5e8f\u5728\u9762\u5411\u5bf9\u8c61\u7684\u8bbe\u8ba1\u4e0a\u5b58\u5728\u95ee\u9898\u3002\u4f46\u5176\u5b9e\u8fd9\u5f88\u4e0d\u597d\u8bf4\uff0c\u8bf4\u4e0d\u5b9a\u8fc7\u4e24\u4e2a\u7248\u672cMockito\u5c31\u6253\u8138\u4e86~<\/p>\n\n\n\n<p><strong>5<strong>\u3001<\/strong>\u4f7f\u7528@Mock\/@InjectMocks\u6ce8\u89e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Mockito\u4e5f\u63d0\u4f9b\u4e86\u6ce8\u89e3\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5bf9\u4f9d\u8d56\u7684\u6253\u6869\u4ee5\u53ca\u6ce8\u5165\uff0c\u4e5f\u5c31\u662f@Mock\u548c@InjectMocks\u6ce8\u89e3\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.mockito.InjectMocks; \nimport org.mockito.Mock;  \nimport org.mockito.MockitoAnnotations;\n\n\tpublic class PortfolioTest {\n\t\t@InjectMocks\n\t\tPortfolio portfolio;\n\t\t@Mock\n\t\tStockService stockService;\n\n\t\t@BeforeEach\n\t\tpublic void setUp() {\n\t\t\tMockitoAnnotations.initMocks(this);\n\t\t}\n\t}\n<\/pre>\n\n\n\n<ul><li>@Mock\u6ce8\u89e3\uff1aMockito \u901a\u8fc7 @mock \u6ce8\u89e3\u6765\u521b\u5efa mock \u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u6765\u4ee3\u66ffMockito.mock()\u65b9\u6cd5\u3002<\/li><li>@InjectMocks\uff1a\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u5c06@Mock\uff08\u6216@Spy\uff09\u6ce8\u89e3\u521b\u5efa\u7684mock\u6ce8\u5165\u5230\u7528\u8be5\u5b9e\u4f8b\u4e2d\u3002<\/li><\/ul>\n\n\n\n<p>\u5728\u4f7f\u7528\u4e86\u8fd9\u4e24\u4e2a\u6ce8\u89e3\u4e4b\u540e\uff0csetup()\u65b9\u6cd5\u4e5f\u53d1\u751f\u4e86\u53d8\u5316\u3002\u989d\u5916\u589e\u52a0\u4e86\u4ee5\u4e0b\u8fd9\u6837\u4e00\u884c\u4ee3\u7801\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=\"\">\tpublic void setUp() {\n\t\tMockitoAnnotations.initMocks(this);\n\t}\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=\"\">\/\/ \u4e0a\u9762\u8fd9\u4e2a\u65b9\u6cd5\u5176\u5b9e\u7b49\u4ef7\u4e8e\u8fd9\u4e2a\n\tpublic void setUp() {\n\n\t\tportfolio = new Portfolio();\n\n\t\tstockService = mock(StockService.class);\n\n\t\tportfolio.setStockService(stockService);\n\n\t}<\/pre>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u901a\u8fc7@Mock\/@InjectMocks \u4ee5\u53cainitMocks\u65b9\u6cd5\u7684\u914d\u5408\uff0cMockito\u5b9e\u73b0\u4e86<\/p>\n\n\n\n<ul><li>@Mock\u5c06\u5916\u90e8\u4f9d\u8d56StockService \u8fdb\u884c\u4e86mock<\/li><\/ul>\n\n\n\n<ul><li>@InjectMocks\u901a\u8fc7\u8c03\u7528Portfolio\u7c7b\u7684\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\u5b8c\u6210\u4e86portfolio\u7684\u5b9e\u4f8b\u5316,\u5e76\u901a\u8fc7Portfolio\u7c7b\u63d0\u4f9b\u7684setStockService()\u65b9\u6cd5\uff0c\u7528setter\u6ce8\u5165\u7684\u65b9\u5f0f,\u5c06\u524d\u8ff0\u88abmock\u7684stockService\u6ce8\u5165\u8fdbportfolio\u3002<\/li><\/ul>\n\n\n\n<p><strong>6. mockito\u600e\u4e48mock\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u5c5e\u6027<\/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=\"\">@InjectMocks\n    private UserServiceImpl userService;\n\n    @Test\n    public void test() throws NoSuchFieldException{\n        Field apiField = UserServiceImpl.class.getDeclaredField(\"username\");\n        \/\/ \u5df2\u7ecf\u4e3aUserServiceImpl\u7c7b\u7684username\u5c5e\u6027\u6210\u529f\u8bbe\u7f6e\u503c\uff1a1\n        FieldSetter.setField(userService, apiField, \"1\");\n     } <\/pre>\n\n\n\n<p><strong>\u8865\u5145\u8bf4\u660e<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u826f\u597d\u7684\u67b6\u6784\u8bbe\u8ba1\u4e0b\uff0c\u638c\u63e1\u4ee5\u4e0a\u7684\u5185\u5bb9\uff0c\u5c31\u53ef\u4ee5\u5199\u51fa\u8986\u76d6\u7387\u5f88\u9ad8\u7684\u6d4b\u8bd5\u6848\u4f8b\u4e86\u3002\u4f46Mockito\u63d0\u4f9b\u7684\u4e0d\u4ec5\u5982\u6b64\u3002<\/p>\n\n\n\n<ul><li>\u8c03\u7528Mockito\u7684mock\u65b9\u6cd5\uff0c\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u662f\u5bf9\u5176\u6240\u6709\u65b9\u6cd5\uff0c\u539f\u65b9\u6cd5\u8fd4\u56de\u4e3a\u57fa\u672c\u7c7b\u578b\u7684\uff0c\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c\uff0c\u5426\u5219\u8fd4\u56denull\u3002<\/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=\"\">public class AppleTest {\n\n    @Test\n\n    public void test_mock_and_spy_method(){\n\n        Apple mockApple = mock(Apple.class);\n\n        Apple spyApple = spy(Apple.class);\n\n        Assertions.assertNull(mockApple.getColor());\n\n        Assertions.assertEquals(\"red\",spyApple.getColor());\n\n    }\n\n}\n\nclass Apple{\n\n    public String getColor(){\n\n        return \"red\";\n\n    }\n\n}<\/pre>\n\n\n\n<p>\u800cspy\u65b9\u6cd5\u5219\u662f\u5bf9\u5b9e\u9645\u5bf9\u8c61\u7684mock\uff0c\u53ef\u4ee5\u4f20\u5165\u5177\u4f53\u7684\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u5982\u4e0a\u4ee3\u7801\u4f20\u5165\u7c7b\uff0c\u5f53\u4f20\u5165\u7c7b\u65f6\uff0c\u5219\u81ea\u52a8\u6784\u9020\u5bf9\u8c61\u3002\u5bf9\u4e8e\u6ca1\u6709mock\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0cspy\u51fa\u6765\u7684\u5bf9\u8c61\u9ed8\u8ba4\u8c03\u7528\u539f\u65b9\u6cd5\u3002<\/p>\n\n\n\n<ul><li>\u6d4b\u8bd5\u6848\u4f8b\u7ed3\u675f\u540e\uff0c\u7ecf\u5e38\u9700\u8981\u6821\u9a8cmock\u5bf9\u8c61\u662f\u5426\u88ab\u6b63\u5e38\u8c03\u8d77\u3002\u5728Mockito1.x\u7684\u65f6\u5019\uff0c\u7ecf\u5e38\u4f1a\u5728\u4ee3\u7801\u8c03\u7528\u7ed3\u675f\u540e\uff0c\u4f7f\u7528verify\u68c0\u67e5\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u4ee3\u7801\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u5e76\u4e0d\u4f1a\u88ab\u4f7f\u7528\u7684mock\u5bf9\u8c61\uff0c\u53ef\u80fd\u662f\u4ece\u5176\u4ed6\u5730\u65b9\u590d\u5236\u8fc7\u6765\u7684\uff0c\u4e5f\u53ef\u80fd\u4e00\u5f00\u59cb\u6784\u601d\u9519\u8bef\uff0c\u4f7f\u5f97\u6d4b\u8bd5\u4ee3\u7801\u5e76\u4e0d\u7b80\u6d01\u3002<\/li><li>\u4f46\u4eceMockito2.1\u5f00\u59cb\uff0cmockito\u8d8b\u5411\u4e25\u683c\u3002\u5f53\u4f7f\u7528<\/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=\"\">@ExtendWith(MockitoExtension.class)<\/pre>\n\n\n\n<p>\u6216\u662fJunit4\u7684MockitoJunitRunner\u65f6\uff0c\u9ed8\u8ba4\u91c7\u7528\u4e25\u683c\u6a21\u5f0f\uff0c\u5f53\u51fa\u73b0\u4e0d\u5fc5\u8981\u7684\u6869\u65f6\uff0c\u5219\u629b\u51fa\u5f02\u5e38\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=\"\">org.mockito.exceptions.misusing.UnnecessaryStubbingException:\n\nUnnecessarystubbings detected.\n\nClean &amp; maintainabletest code requires zero unnecessary code.<\/pre>\n\n\n\n<p>\u5f53\u7136\u4e5f\u53ef\u66f4\u6539\u4e25\u683c\u7a0b\u5ea6\uff0c\u4f46\u63a8\u8350\u8fd8\u662f\u5e94\u8be5\u4fdd\u7559\u6d4b\u8bd5\u4ee3\u7801\u7684\u6574\u6d01\u6027\u3002<\/p>\n\n\n\n<ul><li>\u5f53\u7136\u5373\u4f7f\u6709\u4e25\u683c\u7684\u6821\u9a8cmock\u5bf9\u8c61\u88ab\u8c03\u8d77\u8fc7\uff0c\u4f46\u5b9e\u9645\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u6709\u65f6\u4f1a\u6709\u4e00\u4e9b\u66f4\u5177\u4f53\u7684\u9a8c\u8bc1\u3002Mockito\u4e5f\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u65b9\u6cd5\u3002\u4e3b\u8981\u662f\u4e0b\u9762\u8fd9\u4e9b\uff1a<\/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=\"\">\/\/ \u9a8c\u8bc1\u8c03\u7528\u4e861\u6b21getColor\u65b9\u6cd5\n\nverify(apple,times(1)).getColor();\n\n\/\/ \u9a8c\u8bc1\u81f3\u5c11\u8c03\u7528\u4e861\u6b21getColor\u65b9\u6cd5\n\nverify(apple,atLeastOnce()).getColor();\n\n\/\/ \u9a8c\u8bc1\u4ece\u6765\u6ca1\u6709\u8c03\u7528\u8fc7getWeight\u65b9\u6cd5\n\nverify(apple,never()).getWeight();\n\n\/\/ \u9a8c\u8bc1\u8c03\u7528getColor\u65b9\u6cd5\u8017\u65f6\u4e0d\u8d85\u8fc7100ms\n\nverify(apple,timeout(100)).getColor();<\/pre>\n\n\n\n<p><strong>\u8865\u5145\u4f8b\u5b50\uff1amockito\u6d4b\u8bd5final\u7c7b\/static\u65b9\u6cd5\/\u81ea\u5df1new\u7684\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5148\u51c6\u5907\u51e0\u4e2a\u7c7b\uff0c\u65b9\u4fbf\u540e\u9762\u8bb2\u89e3\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=\"\">public final class FinalSampleUtils {\n\n    public static String foo() {\n        return \"aaa\";\n    }\n\n    public static String bar(String a) {\n        return \"bar:\" + a;\n    }\n}<\/pre>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2afinal\u7c7b\uff0c\u91cc\u9762\u67092\u4e2astatic\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=\"\">public class NewObject {\n\n    public String haha() {\n        return \"haha\";\n    }\n}<\/pre>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u5e73\u6de1\u65e0\u5947\u7684\u7c7b\uff0c\u6ca1\u5565\u597d\u8bf4\u7684\u3002\u5b83\u4fe9\u7684\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\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=\"\">import org.springframework.stereotype.Service;\n\n@Service\npublic class SampleServiceImpl implements SampleService {\n\n    NewObject obj;\n\n    public SampleServiceImpl() {\n        obj = new NewObject();\n    }\n\n    @Override\n    public void helloWorld() {\n        String foo = FinalSampleUtils.foo();\n        String bar = FinalSampleUtils.bar(\"test\");\n\n        System.out.println(\"hello1:\" + foo);\n        System.out.println(\"hello2:\" + bar);\n        System.out.println(\"h:\" + obj.haha());\n    }\n}\n<\/pre>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u666e\u901a\u7684@Service\u5b9e\u73b0\u7c7b\uff0c\u4f46\u67092\u4e2a\u6ce8\u610f\u7684\u5730\u65b9\uff1a<\/p>\n\n\n\n<ul><li>\u91cc\u9762\u7528\u5230\u7684NewObject\uff0c\u5e76\u4e0d\u662f@Autowired\u4e4b\u7c7b\u7531Spring\u6ce8\u5165\u7684\uff0c\u800c\u662f\u81ea\u5df1new\u7684<\/li><li>helloWorld\u91cc\uff0c\u4f7f\u7528\u4e86final\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u4ee5\u53caobj\u7684\u666e\u901a\u65b9\u6cd5\u3002<\/li><\/ul>\n\n\n\n<p>\u57283.4\u4ee5\u4e0b\u7684\u4f4e\u7248\u672cmockito\u4e2d\uff0c\u5982\u679c\u60f3mock helloWorld\u65b9\u6cd5\u662f\u5f88\u56f0\u96be\u7684\uff0c\u4f46\u5728\u9ad8\u7248\u672c\u4e2d\u529f\u80fd\u6709\u6240\u52a0\u5f3a\uff0c\u53c2\u8003\u4e0b\u9762\u7684\u4ee3\u7801\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=\"\">import org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.mockito.*;\nimport org.mockito.junit.MockitoJUnitRunner;\n\nimport static org.mockito.ArgumentMatchers.any;\n\n\n@RunWith(MockitoJUnitRunner.class)\npublic class SampleServiceImplTest {\n\n    @Mock\n    NewObject obj;\n\n    @InjectMocks\n    SampleServiceImpl sampleService;\n\n    @Test\n    public void testHelloWorld() {\n        MockedStatic&lt;FinalSampleUtils> mocked = Mockito.mockStatic(FinalSampleUtils.class);\n\n        \/\/mock\u4e0d\u5e26\u53c2\u6570\u7684static\u65b9\u6cd5\n        mocked.when(FinalSampleUtils::foo).thenReturn(\"bbb\");\n\n        \/\/mock\u5e26\u53c2\u6570\u7684static\u65b9\u6cd5\n        mocked.when(() -> FinalSampleUtils.bar(any())).thenReturn(\"xxx\");\n\n        \/\/mock\u4ee3\u7801\u4e2d\u81ea\u5df1new\u7684\u5b9e\u4f8b\u53ca\u201c\u8be5\u5b9e\u4f8b\u7684\u65b9\u6cd5\u201d\n        MockedConstruction&lt;NewObject> newObjectMocked = Mockito.mockConstruction(NewObject.class);\n        Mockito.when(obj.haha()).thenReturn(\"who am i ?\");\n\n        sampleService.helloWorld();\n\n    }\n}\n<\/pre>\n\n\n\n<p>\u8dd1\u51fa\u6765\u7684\u6548\u679c\u5982\u4e0b\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=\"\">hello1:bbb\nhello2:xxx\nh:who am i ?<\/pre>\n\n\n\n<p>\u4ece\u8f93\u51fa\u4e0a\u770b\uff0c\u4e0d\u7ba1\u662f\u5e26\u53c2\u8fd8\u662f\u4e0d\u5e26\u53c2\u7684static\u65b9\u6cd5\uff0c\u90fd\u6210\u529fmock\uff0c\u8fd4\u56de\u4e86mock\u540e\u7684\u503c\uff0c\u800c\u4e14\u81ea\u5df1new\u7684\u5bf9\u8c61\uff0c\u4e5f\u540c\u6837mock\u6210\u529f\u4e86\u3002<\/p>\n\n\n\n<p>\u6700\u540e<strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\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<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>\u4ee5\u4e0a\u5c31\u662fMockito\u7684\u4e3b\u8981\u73a9\u6cd5\u3002\u5176\u7279\u5f81\u5c31\u662f\u5c31\u662f\u5199\u51fa\u6765\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u6bd4\u8f83\u9ad8\uff0c\u6613\u4e8e\u7406\u89e3\u3002\u5373\u4f7f\u662f\u672a\u63a5\u89e6\u8fc7Mockito\u7684\u4eba\uff0c\u4e5f\u80fd\u5f88\u5feb\u7684\u7406\u89e3\u6bcf\u884c\u4ee3\u7801\u7684\u542b\u4e49\u3002\u5f53\u7136\uff0c\u66f4\u8be6\u7ec6\u7684\u529f\u80fd\uff0c\u770b\u5b98\u65b9\u6587\u6863\u662f\u6700\u597d\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<p>\u5728\u5b66\u4e60Mockito\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5bf9\u5176\u4ed6\u65b9\u9762\u4e5f\u6709\u4e00\u4e9b\u601d\u8003\u3002<\/p>\n\n\n\n<ul><li>\u7b2c\u4e00\u70b9\u662f\uff0c\u4ee5\u524d\u5c31\u542c\u8bf4\uff0cMockito\u529f\u80fd\u4e0a\u4e0d\u652f\u6301\u9759\u6001\u65b9\u6cd5\uff0cfinal\u65b9\u6cd5\u7b49\u3002\u5373\u4f7f\u73b0\u5728\u53bb\u767e\u5ea6\uff0c\u6216\u8005\u8c37\u6b4c\u641c\u7d22\u201c\u5982\u4f55\u7528Mockito mock\u9759\u6001\u65b9\u6cd5\u201d\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u4e5f\u662f\u8ba9\u5f15\u5165PowerMock\uff0c\u4f46\u5176\u5b9e\u8fd9\u4e9b\u529f\u80fd\u5728\u51e0\u5e74\u524d\u5df2\u7ecf\u5728Mockito\u81ea\u8eab\u652f\u6301\u4e86\u3002\u4e5f\u8bb8\u5b66\u4e60\u8fd9\u4e9b\u5185\u5bb9\uff0c\u5b98\u65b9\u6587\u6863\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/li><\/ul>\n\n\n\n<ul><li>\u7b2c\u4e8c\u70b9\u662f\uff0cMockito\u53ea\u662f\u5de5\u5177\u3002\u5176\u5b9e\u7528JMockit\u4e5f\u597d\uff0cEasyMock\u4e5f\u597d\uff0c\u90fd\u53ea\u662f\u5171\u540c\u8fbe\u5230\u4e00\u4e2a\u65b9\u4fbf\u6d4b\u8bd5\u7684\u76ee\u7684\u3002\u800c\u6700\u7ec8\u60f3\u8981\u7684\uff0c\u662f\u60f3\u8981\u5199\u51fa\u4e00\u4e2a\u771f\u6b63\u80fd\u201c\u8dd1\u5b8c\u6d4b\u8bd5\u811a\u672c\u4e4b\u540e\uff0c\u5c31\u80fd\u5f88\u653e\u5fc3\u8ba4\u4e3a\u8fd9\u6b21\u7684\u4fee\u6539\u6ca1\u6709\u95ee\u9898\u201d\u7684\u4ee3\u7801\u3002\u5982\u4f55\u8bbe\u8ba1\u6d4b\u8bd5\u6848\u4f8b\uff0c\u8fd9\u662f\u5173\u952e\u6240\u5728\u3002\u800c\u53e6\u4e00\u65b9\u9762\uff0c\u4e00\u4e2a\u7c7b\u662f\u5426\u6613\u4e8e\u6d4b\u8bd5\uff0c\u662f\u5426\u5728\u6574\u4f53\u67b6\u6784\u8bbe\u8ba1\u4e4b\u65f6\uff0c\u5c31\u5df2\u7ecf\u51b3\u5b9a\u597d\u4e86\u5462\uff1f<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u56e0\u4e3a\u5355\u5143\u6d4b\u8bd5\u7684\u9700\u8981\uff0c\u7ecf\u5e38\u4f1a\u9700\u8981mock\u6389\u4e00\u4e9b\u5916\u90e8\u5f15\u7528\u6216\u8005\u6570\u636e\u5e93\u63a5\u5165\u7b49\u5185\u5bb9\u3002\u63a5\u89e6\u8fc7JMockit\u3001Mock [&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\/6586"}],"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=6586"}],"version-history":[{"count":19,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6586\/revisions"}],"predecessor-version":[{"id":7766,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6586\/revisions\/7766"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}