{"id":6662,"date":"2022-07-26T14:17:42","date_gmt":"2022-07-26T06:17:42","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6662"},"modified":"2022-07-26T14:17:42","modified_gmt":"2022-07-26T06:17:42","slug":"mockito%e7%9a%84%e4%bd%bf%e7%94%a8%e5%8f%8a%e5%8e%9f%e7%90%86%e5%8f%8a%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6662","title":{"rendered":"Mockito\u7684\u4f7f\u7528\u53ca\u539f\u7406\u53ca\u5206\u6790"},"content":{"rendered":"\n<p>\u672c\u6587\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<ul><li>\u5982\u4f55\u4f7f\u7528Mockito\u5199\u5355\u5143\u6d4b\u8bd5<\/li><li>Mockito\u5b9e\u73b0\u539f\u7406\u6d45\u6790<\/li><li>\u6a21\u4effMockito\u5b9e\u73b0mock\u529f\u80fd<\/li><\/ul>\n\n\n\n<p><strong>\u524d\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e0a\u4e00\u7bc7\u8bb2\u8ff0\u4e86\u5982\u4f55\u7f16\u5199\u5065\u58ee\u7684\u5355\u5143\u6d4b\u8bd5\uff0c\u5176\u4e2d\u89e3\u51b3\u5916\u90e8\u6570\u636e\u4f9d\u8d56\u7684\u65b9\u5f0f\u5c31\u662fMock\u6570\u636e\u8fd4\u56de\uff0c\u90a3\u4e48\u5177\u4f53\u5982\u4f55Mock\u6570\u636e\u5462\uff1f\u5176\u5b9e\u73b0\u673a\u5236\u53c8\u662f\u600e\u6837\u7684\u5462\uff1f<\/p>\n\n\n\n<p>Mock\u6700\u5e38\u7528\u7684\u6846\u67b6\u4e4b\u4e00\u662fMockito\uff0c\u4ee5\u4e0b\u5206\u6790\u90fd\u5c06\u57fa\u4e8eMockito\u5c55\u5f00\u3002<\/p>\n\n\n\n<p><strong>Mockito<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e00\u6b21\u5b8c\u6574\u7684Mock\uff0c\u5305\u62ec<\/p>\n\n\n\n<ul><li>\u8bbe\u5b9a\u76ee\u6807\u8bbe\u7f6e<\/li><li>\u6d88\u8d39\u6761\u4ef6<\/li><li>\u9884\u671f\u8fd4\u56de\u7ed3\u679c<\/li><li>\u6d88\u8d39\u5e76\u68c0\u9a8c\u8fd4\u56de\u7ed3\u679c<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u9996\u5148\u770b\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u7684\u4f8b\u5b50\u6765\u770b\u4e0b\u5982\u4f55\u4f7f\u7528Mockito\u6765\u8fdb\u884cMock\u6570\u636e\u8fd4\u56de\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=\"\"> when(productService.getProductInfo(any())).thenAnswer(invocationOnMock -> {\n                List&lt;ProductInfo> productInfos = new ArrayList&lt;>();\n                return productInfos;\n            });\n    \n    \/\/...        \n    List&lt;ProductInfo> productInfos  = productService.getProductInfo(1);<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u8fd9\u4e9b\u6761\u4ef6\u662f\u5982\u4f55\u5bf9\u5e94\u7684\u5462\uff1f<\/p>\n\n\n\n<ul><li>\u8bbe\u5b9a\u76ee\u6807 &gt; List&lt;ProductInfo&gt; productInfos<\/li><li>\u8bbe\u7f6e\u6d88\u8d39\u6761\u4ef6 -&gt; productService.getProductInfo(any())<\/li><li>\u9884\u671f\u8fd4\u56de\u7ed3\u679c -&gt; thenAnswer(&#8230;)<\/li><li>\u6d88\u8d39\u5e76\u68c0\u9a8c\u8fd4\u56de\u7ed3\u679c -&gt; productService.getProductInfo(1)<\/li><\/ul>\n\n\n\n<p>\u6240\u4ee5\u4e0d\u96be\u53d1\u73b0\uff0cMockito\u4e3b\u8981\u5c31\u662f\u901a\u8fc7Stub\u6253\u6869\uff0c\u901a\u8fc7\u65b9\u6cd5\u540d\u52a0\u53c2\u6570\u6765\u51c6\u786e\u7684\u5b9a\u4f4d\u6d4b\u8bd5\u6869\u7136\u540e\u8fd4\u56de\u9884\u671f\u7684\u503c<\/p>\n\n\n\n<p><strong>\u63d0\u51fa\u7591\u95ee<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6211\u4eec\u90fd\u77e5\u9053\uff0cJava\u7684\u7a0b\u5e8f\u8c03\u7528\u662f\u7528\u5806\u6808\u6765\u5b9e\u73b0\u7684\uff0c\u90a3\u4e48\u662f\u4e0d\u662f\u6709\u8fd9\u6837\u7684\u7591\u95ee\uff1a<\/p>\n\n\n\n<p>when()\u6d88\u8d39\u7684\u5e94\u8be5\u662fproductService.getProductInfo()\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5bf9\u5176\u5185\u90e8\u5b9e\u73b0\u5e76\u4e0d\u611f\u77e5\u7684\uff0c\u90a3\u4e48\u5b83\u662f\u5982\u4f55\u6765\u51c6\u786e\u901a\u8fc7\u51fd\u6570\u540d\u52a0\u53c2\u6570\u7684\u6761\u4ef6\u6765\u6253\u6869\u7684\u5462\uff1f<\/p>\n\n\n\n<p><strong>Mockito\u7684\u5b9e\u73b0\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5e94\u8be5\u77e5\u9053\uff0cMock\u672c\u8d28\u4e0a\u662f\u4e00\u4e2aProxy\u4ee3\u7406\u6a21\u5f0f\u7684\u5e94\u7528\u3002<\/p>\n\n\n\n<p>Proxy\u6a21\u5f0f\uff0c\u662f\u5728\u5bf9\u8c61\u63d0\u4f9b\u4e00\u4e2aproxy\u5bf9\u8c61\uff0c\u6240\u6709\u5bf9\u771f\u5b9e\u5bf9\u8c61\u7684\u8c03\u7528\uff0c\u90fd\u5148\u7ecf\u8fc7proxy\u5bf9\u8c61\uff0c\u7136\u540e\u7531proxy\u5bf9\u8c61\u6839\u636e\u60c5\u51b5\uff0c\u51b3\u5b9a\u76f8\u5e94\u7684\u5904\u7406\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u505a\u4e00\u4e2a\u81ea\u5df1\u7684\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u518d\u8c03\u7528\u771f\u5b9e\u5bf9\u8c61\u5bf9\u5e94\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5Mockito\u672c\u8d28\u4e0a\u5c31\u662f\u5728\u4ee3\u7406\u5bf9\u8c61\u8c03\u7528\u65b9\u6cd5\u524d\uff0c\u7528stub\u7684\u65b9\u5f0f\u8bbe\u7f6e\u5176\u8fd4\u56de\u503c\uff0c\u7136\u540e\u5728\u771f\u5b9e\u8c03\u7528\u65f6\uff0c\u7528\u4ee3\u7406\u5bf9\u8c61\u8fd4\u56de\u8d77\u9884\u8bbe\u7684\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p><strong>\u9a8c\u8bc1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u67e5\u770bwhen()\u6e90\u7801<\/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 &lt;T> OngoingStubbing&lt;T> when(T methodCall) {\n        MockingProgress mockingProgress = mockingProgress();\n        mockingProgress.stubbingStarted();\n        @SuppressWarnings(\"unchecked\")\n        OngoingStubbing&lt;T> stubbing = (OngoingStubbing&lt;T>) mockingProgress.pullOngoingStubbing();\n        if (stubbing == null) {\n            mockingProgress.reset();\n            throw missingMethodInvocation();\n        }\n        return stubbing;\n    }<\/pre>\n\n\n\n<p>\u53d1\u73b0\u6240\u6709\u7684methodCall\u6216\u88ab\u8f6c\u6362\u6210OngoingStubbing\u5bf9\u8c61\uff0c\u800cOngoingStubbing\u5b58\u50a8\u4e86\u54ea\u4e9b\u4fe1\u606f\u5462\uff1f<\/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 Object handle(Invocation invocation) throws Throwable {\n     if (invocationContainerImpl.hasAnswersForStubbing()) {\n         ...\n    }\n     ...\n     InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(\n            mockingProgress.getArgumentMatcherStorage(),\n           invocation\n    );\n   mockingProgress.validateState();\n    \/\/ if verificationMode is not null then someone is doing verify()\n   if (verificationMode != null) {\n   ...\n      }\n    \/\/ prepare invocation for stubbing   invocationContainerImpl.setInvocationForPotentialStubbing(invocationMatcher);\n  OngoingStubbingImpl&lt;T> ongoingStubbing = \n  new OngoingStubbingImpl&lt;T>(invocationContainerImpl);\nmockingProgress.reportOngoingStubbing(ongoingStubbing);\n   ...\n}\n<\/pre>\n\n\n\n<p>\u67e5\u770b\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u53ef\u4ee5\u53d1\u73b0\u65b9\u6cd5\u8c03\u7528\u7684\u4fe1\u606f(invocation)\u5bf9\u8c61\u88ab\u7528\u6765\u6784\u9020invocationMatcher\u5bf9\u8c61\uff0c\u6700\u7ec8\u4f20\u9012\u7ed9\u4e86ongoingStubbing\u5bf9\u8c61\u3002\u5b8c\u6210\u4e86stub\u4fe1\u606f\u7684\u4fdd\u5b58\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5Mockito\u5728\u6784\u9020\u65f6\uff0c\u4e0d\u4ec5\u4ec5\u4fdd\u5b58\u4e86\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\uff0c\u8fd8\u505a\u4e86\u5927\u91cf\u5904\u7406\uff0c\u4fdd\u5b58\u4e86stub\u7684\u8c03\u7528\u4fe1\u606f\uff0c\u624d\u80fd\u51c6\u786e\u5b9a\u4f4d\u3002<\/p>\n\n\n\n<p>\u800c\u67e5\u770bthenAnswer\u7684\u4ee3\u7801\uff0c\u53d1\u73b0\u4e86\u8fd9\u6837\u7684Demo:<\/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 Integer answer(InvocationOnMock invocation) throws Throwable {\n        return (Integer) invocation.getArguments()[0];\n    }\n<\/pre>\n\n\n\n<p>\u90a3\u4e48\u6211\u4eec\u5c31\u5e94\u8be5\u53ef\u4ee5\u5728answer\u4e2d\u62ff\u5230invocation\u7684\u4fe1\u606f\u554a\uff0c\u4e8e\u662f\u6211\u5c1d\u8bd5\u4e86\u4e00\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=\"\">@Test\n    public void test2() {\n        LinkedList mock = mock(LinkedList.class);\n        when(mock.add(anyString())).thenAnswer(new Answer() {\n            public Object answer(InvocationOnMock invocation) {\n                Object[] args = invocation.getArguments();\n                Object mock = invocation.getMock();\n                return \"called with arguments: \" + args;\n            }\n        });\n\n        System.out.println(mock.add(\"foo\"));\n    }\n<\/pre>\n\n\n\n<p>\u53d1\u73b0\u786e\u5b9e\u53ef\u4ee5\u5728\u65b9\u6cd5\u4e2d\u62ff\u5230\u8c03\u7528\u51fd\u6570\u4ee5\u53ca\u53c2\u6570\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u5728Mockiton\u4e2d\uff0c\u5728\u8bbe\u7f6e\u6761\u4ef6\u65f6\uff0cMockito\u5e76\u4e0d\u662f\u5bf9\u5185\u90e8\u5b9e\u73b0\u4e0d\u611f\u77e5\uff0c\u76f8\u53cd\uff0c\u4fdd\u5b58\u4e86\u53c2\u6570\u540d\u4ee5\u53ca\u5165\u53c2\u4fe1\u606f\uff0c\u6700\u7ec8\u6765\u6784\u5efastub\uff0c\u8fd4\u56de\u4fe1\u606f\u3002<\/p>\n\n\n\n<p><strong>\u81ea\u5df1\u52a8\u624b\u5199Mockito Demo<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5b66\u4e60\u4e86Mockito\u5b9e\u73b0\u539f\u7406\u4e4b\u540e\uff0c\u53d1\u73b0\u5176\u5b9e\u5b83\u672c\u8d28\u4e0a\u5c31\u662f\u901a\u8fc7\u4ee3\u7406 + \u53cd\u6a21\u5f0f\u6253\u6869\u5b9e\u73b0\u7684\u3002\u90a3\u4e48\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2aMockito\u4e48\uff1f<\/p>\n\n\n\n<p>\u53c2\u8003\u76f8\u5173\u8d44\u6599\u540e\uff0c\u53d1\u73b0\u5e94\u8be5\u662f\u53ef\u884c\u7684\uff0c\u5e76\u627e\u5230\u7c7b\u4f3c\u6750\u6599\uff0c\u90a3\u4e48\u8bd5\u8bd5\u5427\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u73b0 mock<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Mock\u7684\u5b9e\u73b0\u5173\u952e\u662f\uff0c\u5b9e\u73b0\u52a8\u6001\u4ee3\u7406\uff0c\u88ab mock \u7684\u5bf9\u8c61\u53ea\u662f\u201c\u5047\u88c5\u201d\u8c03\u7528\u4e86\u8be5\u65b9\u6cd5\uff0c\u7136\u540e\u8fd4\u56de\u5047\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528cglib\u6765\u8fdb\u884c\u52a8\u6001\u4ee3\u7406\u3002\u901a\u8fc7class\u5bf9\u8c61\u521b\u5efa\u8be5\u5bf9\u8c61\u7684\u52a8\u6001\u4ee3\u7406\u5bf9\u8c61\uff0c\u7136\u540e\u8bbe\u7f6e\u8be5\u5bf9\u8c61\u7684\u7236\u7c7b\u4e0e\u56de\u8c03\u5373\u53ef\u3002\u5e76\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u5b9a\u4e49\u62e6\u622a\u5668\uff0c\u5b9e\u73b0\u81ea\u5b9a\u4e49\u903b\u8f91\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 Mockito {\n\n    \/**\n     * \u6839\u636eclass\u5bf9\u8c61\u521b\u5efa\u8be5\u5bf9\u8c61\u7684\u4ee3\u7406\u5bf9\u8c61 \n     * 1\u3001\u8bbe\u7f6e\u7236\u7c7b\uff1b2\u3001\u8bbe\u7f6e\u56de\u8c03 \n     * \u672c\u8d28\uff1a\u52a8\u6001\u521b\u5efa\u4e86\u4e00\u4e2aclass\u5bf9\u8c61\u7684\u5b50\u7c7b \n     *\n     * @return\n     *\/\n    public static &lt;T> T mock(Class&lt;T> clazz) {\n        Enhancer enhancer = new Enhancer();\n        enhancer.setSuperclass(clazz);\n        enhancer.setCallback(new MockInterceptor());\n        return (T)enhancer.create();\n    }\n\n    private static class MockInterceptor implements MethodInterceptor {\n        public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {\n            return null;\n        }\n    }\n}\n<\/pre>\n\n\n\n<p><strong>\u5b9e\u73b0 stub<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u6765\u8868\u793a\u5bf9\u51fd\u6570\u7684\u8c03\u7528\uff0c\u91cd\u5199equals()\u65b9\u6cd5\uff0c\u901a\u8fc7\u51fd\u6570\u540d + \u53c2\u6570\u5217\u8868\u6765\u5224\u65ad\u8c03\u7528\u662f\u5426\u76f8\u540c\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 Invocation {\n    private final Object mock;\n    private final Method method;\n    private final Object[] arguments;\n    private final MethodProxy proxy;\n\n    public Invocation(Object mock, Method method, Object[] args, MethodProxy proxy) {\n        this.mock = mock;\n        this.method = method;\n        this.arguments = copyArgs(args);\n        this.proxy = proxy;\n    }\n\n    private Object[] copyArgs(Object[] args) {\n        Object[] newArgs = new Object[args.length];\n        System.arraycopy(args, 0, newArgs, 0, args.length);\n        return newArgs;\n    }\n\n    @Override\n    public boolean equals(Object obj) {\n        if (obj == null || !obj.getClass().equals(this.getClass())) { return false; }\n        Invocation other = (Invocation)obj;\n        return this.method.equals(other.method) &amp;&amp; this.proxy.equals((other).proxy)\n            &amp;&amp; Arrays.deepEquals(arguments, other.arguments);\n    }\n\n    @Override\n    public int hashCode() {\n        return 1;\n    }\n}<\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u5728 MockInterceptor \u7c7b\u4e2d\uff0c\u9700\u8981\u505a\u4e24\u4e2a\u64cd\u4f5c\u3002<\/p>\n\n\n\n<ul><li>\u4e3a\u4e86\u8bbe\u7f6e\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\uff0c\u9700\u8981\u5b58\u653e\u5bf9\u65b9\u6cd5\u7684\u5f15\u7528\uff08Invocation\uff09<\/li><li>\u8c03\u7528\u65b9\u6cd5\u65f6\uff0c\u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u8bbe\u7f6e\u4e86\u8be5\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\uff08results\uff09\u3002\u5982\u679c\u8bbe\u7f6e\u4e86\uff0c\u5219\u8fd4\u56de\u8be5\u503c\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 Mockito {\n\n    private static Map&lt;Invocation, Object> results = new HashMap&lt;Invocation, Object>();\n    private static Invocation lastInvocation;\n\n    public static &lt;T> T mock(Class&lt;T> clazz) {\n        Enhancer enhancer = new Enhancer();\n        enhancer.setSuperclass(clazz);\n        enhancer.setCallback(new MockInterceptor());\n        return (T)enhancer.create();\n    }\n\n    private static class MockInterceptor implements MethodInterceptor {\n        public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {\n            Invocation invocation = new Invocation(proxy, method, args, proxy);\n            lastInvocation = invocation;\n            if (results.containsKey(invocation)) {\n                return results.get(invocation);\n            }\n            return null;\n        }\n    }\n\n    public static &lt;T> When&lt;T> when(T o) {\n        return new When&lt;T>();\n    }\n\n    public static class When&lt;T> {\n        public void thenReturn(T retObj) {\n            results.put(lastInvocation, retObj);\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6d4b\u8bd5\u7528\u4f8b\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=\"\">@Test\n    public void test() {\n        Calculate calculate = mock(Calculate.class);\n        when(calculate.add(1, 1)).thenReturn(1);\n        Assert.assertEquals(1, calculate.add(1, 1));\n    }<\/pre>\n\n\n\n<p><strong>\u5176\u4ed6<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Mockito\u6253\u6869\u8fd4\u56de\u7684\u65b9\u5f0f\u6709\u5f88\u591a\uff0c\u8fd9\u8fb9\u4e3b\u8981\u5173\u6ce8\u4e86\u7ecf\u5e38\u4f7f\u7528\u7684thenAnswer()\u51fd\u6570\uff0c\u81f3\u4e8e\u5176\u4ed6thenReturn()\u3001thenThrow()\u3001thenCallRealMethod()\u3001then()\u51fd\u6570\uff0c\u57fa\u672c\u7c7b\u4f3c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a \u5982\u4f55\u4f7f\u7528Mockito\u5199\u5355\u5143\u6d4b\u8bd5 Mockito\u5b9e\u73b0\u539f\u7406\u6d45\u6790 \u6a21\u4effMockito\u5b9e\u73b0moc [&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\/6662"}],"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=6662"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6662\/revisions"}],"predecessor-version":[{"id":6663,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6662\/revisions\/6663"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}