{"id":985,"date":"2022-05-26T00:41:44","date_gmt":"2022-05-26T08:41:44","guid":{"rendered":"https:\/\/ligai.cn\/blog\/?p=985"},"modified":"2022-05-26T18:41:10","modified_gmt":"2022-05-27T02:41:10","slug":"%e6%8a%80%e6%9c%af%e5%88%86%e4%ba%ab-javaer-%e5%a6%82%e4%bd%95%e5%81%9a%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/ligai.cn\/blog\/team\/985.html","title":{"rendered":"\u6280\u672f\u5206\u4eab | Javaer \u5982\u4f55\u505a\u5355\u5143\u6d4b\u8bd5\uff1f"},"content":{"rendered":"\n<p><strong>\u524d\u8a00\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u672c\u6587\u9002\u7528\u4e8ejavaer\uff0c\u5176\u4ed6\u5f00\u53d1\u8005\u6216\u8bb8\u53ef\u4ee5\u501f\u9274\u3002<\/strong><\/p>\n\n\n\n<p>\u5199\u672c\u6587\u7684\u4e3b\u65e8\u6709\u4e24\u4e2a\uff0c\u4e00\u662f\u7b80\u5355\u7684\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e0b\u5355\u5143\u6d4b\u8bd5\uff0c\u4e8c\u662f\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6765\u4ecb\u7ecd\u4e00\u4e9b\u5355\u5143\u6d4b\u8bd5\u7684\u6280\u5de7\uff0c\u5e0c\u671b\u4ee5\u6b64\u6765\u964d\u4f4e\u5927\u5bb6\u5199\u5355\u5143\u6d4b\u8bd5\u7684\u95e8\u69db\u3002<\/p>\n\n\n\n<h2><strong>1\u3001\u5355\u5143\u6d4b\u8bd5\u7684\u5b9a\u4e49<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5355\u5143\u6d4b\u8bd5\u901a\u5e38\u662f\u7531\u8f6f\u4ef6\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u548c\u8fd0\u884c\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\uff08\u79f0\u4e3a\u201c\u5355\u5143\u201d\uff09\u7b26\u5408\u5176\u8bbe\u8ba1\u5e76\u6309\u9884\u671f\u8fd0\u884c\u3002\u5728\u8fc7\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u4e00\u4e2a\u5355\u5143\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u6a21\u5757\uff0c\u4f46\u66f4\u5e38\u89c1\u7684\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u6216\u8fc7\u7a0b\u3002\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u4e00\u4e2a\u5355\u5143\u901a\u5e38\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u63a5\u53e3\uff0c\u4f8b\u5982\u4e00\u4e2a\u7c7b\uff0c\u6216\u8005\u4e00\u4e2a\u5355\u72ec\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u9996\u5148\u4e3a\u6700\u5c0f\u7684\u53ef\u6d4b\u8bd5\u5355\u5143\u7f16\u5199\u6d4b\u8bd5\uff0c\u7136\u540e\u662f\u5b83\u4eec\u4e4b\u95f4\u7684\u590d\u5408\u884c\u4e3a\uff0c\u53ef\u4ee5\u4e3a\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u6784\u5efa\u5168\u9762\u7684\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>Unit tests are typically automated tests written and run by software developers to ensure that a section of an application (known as the &#8220;unit&#8221;) meets its design and behaves as intended. In procedural programming, a unit could be an entire module, but it is more commonly an individual function or procedure. In object-oriented programming, a unit is often an entire interface, such as a class, or an individual method. By writing tests first for the smallest testable units, then the compound behaviors between those, one can build up comprehensive tests for complex applications.<\/p>\n\n\n\n<p class=\"has-text-align-right\">\u2014\u2014Wikipedia,&nbsp;Unit testing<\/p>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5355\u5143\u6d4b\u8bd5\u662f\u9488\u5bf9\u4e00\u4e2a\u5355\u5143\u7f16\u5199\u6d4b\u8bd5\u65b9\u6cd5\u3002\u5176\u4e2d\u7684\u5355\u5143\u53ef\u4ee5\u662f\u4e00\u4e2a\u5f88\u5355\u7eaf\u7684\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u63a5\u53e3\uff0c\u8be5\u63a5\u53e3\u4e2d\u53ef\u4ee5\u5305\u542b\u5404\u79cd\u5176\u4ed6\u51fd\u6570\u7684\u8c03\u7528\u3002<\/p>\n\n\n\n<h2><strong>2\u3001\u5355\u5143\u6d4b\u8bd5\u7528\u4f8b<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u8be5\u9879\u76ee\u7684 SpringBoot \u7248\u672c\u662f 2.2.5.RELEASE\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n&lt;parent&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starters&lt;\/artifactId&gt;\n    &lt;version&gt;2.2.5.RELEASE&lt;\/version&gt;\n&lt;\/parent&gt;<\/code><\/pre>\n\n\n\n<p><strong>\u27ae <span class=\"has-inline-color has-black-color\">2-1.\u9879\u76ee\u6587\u4ef6\u51c6\u5907<\/span><\/strong><\/p>\n\n\n\n<p>\u5047\u8bbe\u6709\u9879\u76ee\u6587\u4ef6\u7684\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"597\" height=\"381\" src=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2022\/05\/798.png\" alt=\"\" class=\"wp-image-986\" srcset=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2022\/05\/798.png 597w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2022\/05\/798-300x191.png 300w\" sizes=\"(max-width: 597px) 100vw, 597px\" \/><figcaption>\u5176\u4e2d\u4f9d\u8d56\u5173\u7cfb\u4e3a:<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>java-study-web-provider \u4f9d\u8d56 java-study-web-api, java-study-common-provider\njava-study-web-api \u4f9d\u8d56 java-study-common-api\njava-study-common-provider \u4f9d\u8d56 java-study-web-api, java-study-common-api<\/code><\/pre>\n\n\n\n<p>\u5728 java-study-web-api \u5305\u4e2d\u6709\u4e2arpc \u5305\uff0c\u5176\u4e2d\u6709\u4e24\u4e2a rpc \u63a5\u53e3\uff0c\u5206\u522b\u662f&nbsp;&nbsp;<\/p>\n\n\n\n<p>WebRpc.class &amp; WebRpc2.class\u3002\u7136\u800c\uff0c\u8fd9\u4e24\u4e2a\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\u5728 java-study-web-provider \u5305\u4e2d\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"578\" height=\"862\" src=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2022\/05\/677.png\" alt=\"\" class=\"wp-image-987\" srcset=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2022\/05\/677.png 578w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2022\/05\/677-201x300.png 201w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface WebRpc {\n\n    ApiResult&lt;String&gt; get();\n\n    ApiResult&lt;String&gt; get2(String param);\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface WebRpc2 {\n\n    ApiResult&lt;String&gt; get();\n\n    ApiResult&lt;String&gt; get(String param);\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class WebRpcImpl implements WebRpc {\n\n    @Override\n    public ApiResult&lt;String&gt; get() {\n        return ApiResult.success(\"get success\");\n    }\n\n    @Override\n    public ApiResult&lt;String&gt; get2(String param) {\n        return ApiResult.success(param);\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class WebRpc2Impl implements WebRpc2 {\n\n    @Override\n    public ApiResult&lt;String&gt; get() {\n        return ApiResult.success(\"get success\");\n    }\n\n    @Override\n    public ApiResult&lt;String&gt; get(String param) {\n        return null;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728 java-study-common-provider \u5305\u4e2d\u6709\u4e2a service \u5305\uff0c\u5176\u4e2d\u6709\u4e24\u4e2a service \u63a5\u53e3\u4ee5\u53ca\u5bf9\u5e94\u7684\u5b9e\u73b0\u7c7b\uff0c\u5206\u522b\u662f CommonEntityService.class, CommonEntityService2.class, CommonEntityServiceImpl.class, CommonEntityService2Impl.class\uff0c\u5728\u4e24\u4e2a\u5b9e\u73b0\u7c7b\u4e2d\u90fd\u6709\u5f15\u7528 rpc \u63a5\u53e3\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"561\" height=\"601\" src=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2022\/05\/56.png\" alt=\"\" class=\"wp-image-989\" srcset=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2022\/05\/56.png 561w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2022\/05\/56-280x300.png 280w\" sizes=\"(max-width: 561px) 100vw, 561px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface CommonEntityService {\n\n    ApiResult&lt;Void&gt; test(CommonEntity commonEntity);\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface CommonEntityService2 {\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class CommonEntityServiceImpl implements CommonEntityService {\n\n    private final Logger logger = LoggerFactory.getLogger(this.getClass());\n\n    @Autowired\n    private CommonEntityManager commonEntityManager;\n    @Autowired\n    private WebRpc webRpc;\n\n\n    @Override\n    public ApiResult&lt;Void&gt; test(CommonEntity commonEntity) {\n        \/\/ webRpc \u5355\u5143\u6d4b\u8bd5\u65f6\u53ef\u80fd\u4e3anull\n        ApiResult&lt;String&gt; getRpc = webRpc.get();\n        if (!getRpc.getSuccess()) {\n            logger.info(\"getRpc fail: {}\", getRpc);\n            return ApiResult.error(getRpc);\n        }\n        ApiResult&lt;String&gt; getRpc2 = webRpc.get2(\"test\");\n        if (!getRpc2.getSuccess()) {\n            logger.info(\"getRpc2 fail: {}\", getRpc2);\n            return ApiResult.error(getRpc2);\n        }\n        \/\/ \u4f9d\u8d56\u8fdc\u7a0b\u65b9\u6cd5\u8c03\u7528\u7ed3\u679c\n        Optional&lt;String&gt; remoteResultOpt = RmiUtil.getRemoteResult();\n        if (!remoteResultOpt.isPresent()) {\n            logger.info(\"getRemoteResult fail\");\n            return ApiResult.error(BizRespStatusEnum.SYS_ERR);\n        }\n        \/\/ \u5165\u5e93\n        int insertNo = commonEntityManager.insert(commonEntity);\n        logger.info(\"insert {} common entity\", insertNo);\n        return ApiResult.success(null);\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class CommonEntityService2Impl implements CommonEntityService2 {\n\n    @Autowired\n    private WebRpc2 webRpc2;\n}<\/code><\/pre>\n\n\n\n<p> <strong>\u27ae<\/strong> <strong>2-2.\u9488\u5bf9 CommonEntityService.class \u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>\u5148\u52a0\u5165 SpringBootTest \u4f9d\u8d56\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;\n    &lt;scope&gt;test&lt;\/scope&gt;\n    &lt;exclusions&gt;\n        &lt;exclusion&gt;\n            &lt;groupId&gt;org.junit.vintage&lt;\/groupId&gt;\n            &lt;artifactId&gt;junit-vintage-engine&lt;\/artifactId&gt;\n        &lt;\/exclusion&gt;\n    &lt;\/exclusions&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u5bf9\u5e94\u7684\u5355\u5143\u6d4b\u8bd5\u7c7b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@ExtendWith(SpringExtension.class)\n@SpringBootTest(classes = CommonTestApplication.class)\npublic class CommonEntityServiceTest {\n\n    private final Logger logger = LoggerFactory.getLogger(this.getClass());\n\n    @Autowired\n    private CommonEntityService commonEntityService;\n\n    @Test\n    public void test() {\n        ApiResult&lt;Void&gt; testSuccess = commonEntityService.test(new CommonEntity());\n        Assert.isTrue(testSuccess.getSuccess(), \"testSuccess fail\");\n        logger.info(\"testSuccess: {}\", JSON.toJSONString(testSuccess));\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u53bb\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u7684 test() \u65b9\u6cd5\u65f6\uff0c\u4f1a\u51fa\u73b0 <span class=\"has-inline-color has-vivid-red-color\">NoSuchBeanDefinitionException<\/span> \u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.peng.java.study.web.api.rpc.WebRpc2' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}\n        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695)\n        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253)\n        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)\n        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)\n        ... 43 more<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u7684\u8fd9\u4e2a\u6a21\u5757\u867d\u7136\u4f9d\u8d56\u4e86 java-study-web-api \u5305\uff0c\u80fd\u591f\u8c03\u7528 rpc \u65b9\u6cd5\uff0c\u4f46\u662f\u6ca1\u6709\u4f9d\u8d56 java-study-web-provider \u5305\uff0c\u6ca1\u529e\u6cd5\u6ce8\u5165\u5bf9\u5e94\u7684\u5b9e\u73b0\u7c7b\u3002 \u67093\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n\n\n\n<p> <strong>I.\u5c06\u8be5\u5355\u5143\u6d4b\u8bd5\u7c7b\u632a\u5230 java-study-web-provider \u5305\u4e2d\uff0c\u8fd9\u6837\u5c31\u80fd\u52a0\u8f7d\u5230\u6240\u6709\u7684 bean \u4e86\u3002<\/strong> \u8fd9\u4e2a\u65b9\u6cd5\u6709\u5c40\u9650\u6027\uff0c\u6bcf\u6b21\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u90fd\u9700\u8981\u52a0\u8f7d\u6240\u6709\u6a21\u5757\u7684\u6587\u4ef6\uff0c\u5927\u5927\u7684\u964d\u4f4e\u4e86\u5355\u5143\u6d4b\u8bd5\u7684\u6548\u7387\u3002<\/p>\n\n\n\n<p> <strong>II .\u5728\u6ce8\u5165rpc\u7684\u6ce8\u89e3 @Autowired \u4e0a\u52a0\u4e0a required = false<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Autowired(required = false)\nprivate WebRpc2 webRpc2;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u6709\u5c40\u9650\u6027\uff0c\u5047\u8bbe\u6bcf\u6b21\u65b0\u589e\u7684 service \u7c7b\u90fd\u9700\u8981\u6ce8\u5165\u540c\u4e00\u4e2a rpc \u65f6\uff0c\u90a3\u6bcf\u4e2a rpc \u7684\u6ce8\u89e3 @Autowired \u90fd\u9700\u8981\u4f7f\u7528 required = false\uff0c\u4e0d\u7136\u5c31\u6ca1\u529e\u6cd5\u542f\u52a8\u5355\u5143\u6d4b\u8bd5\uff0c\u7531\u6b64\u53ef\u89c1\u662f\u6bd4\u8f83\u9ebb\u70e6\u7684\u3002<\/p>\n\n\n\n<p> <strong>III.\u4f7f\u7528Mock\uff0c\u5728\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u524d\uff0c\u5c06\u4f9d\u8d56\u4f46\u53c8\u6ca1\u529e\u6cd5\u83b7\u53d6\u5230\u5b9e\u73b0\u7c7b\u7684 bean \u6ce8\u5165\u8fdb\u53bb\u3002<\/strong> <\/p>\n\n\n\n<p>\u5c06mokito\u5305\u52a0\u5165\u9879\u76ee\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.mockito\/mockito-inline --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.mockito&lt;\/groupId&gt;\n    &lt;artifactId&gt;mockito-inline&lt;\/artifactId&gt;\n    &lt;version&gt;4.5.1&lt;\/version&gt;\n    &lt;scope&gt;test&lt;\/scope&gt;\n&lt;\/dependency&gt;\n\n&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.mockito\/mockito-core --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.mockito&lt;\/groupId&gt;\n    &lt;artifactId&gt;mockito-core&lt;\/artifactId&gt;\n    &lt;version&gt;4.5.1&lt;\/version&gt;\n    &lt;scope&gt;test&lt;\/scope&gt;\n&lt;\/dependency&gt;\n\n&lt;!-- https:\/\/mvnrepository.com\/artifact\/net.bytebuddy\/byte-buddy-agent --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;net.bytebuddy&lt;\/groupId&gt;\n    &lt;artifactId&gt;byte-buddy-agent&lt;\/artifactId&gt;\n    &lt;version&gt;1.12.9&lt;\/version&gt;\n    &lt;scope&gt;test&lt;\/scope&gt;\n&lt;\/dependency&gt;\n\n&lt;!-- https:\/\/mvnrepository.com\/artifact\/net.bytebuddy\/byte-buddy --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;net.bytebuddy&lt;\/groupId&gt;\n    &lt;artifactId&gt;byte-buddy&lt;\/artifactId&gt;\n    &lt;version&gt;1.12.8&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528 @MockBean \u548c MockitoAnnotations.<em>openMocks<\/em>(this) \u53ef\u4ee5\u5c06\u4f9d\u8d56\u7684 bean \u6ce8\u5165\u8fdb\u53bb\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@ExtendWith(SpringExtension.class)\n@SpringBootTest(classes = CommonTestApplication.class)\npublic class CommonEntityServiceTest {\n\n    private final Logger logger = LoggerFactory.getLogger(this.getClass());\n\n    @Autowired\n    private CommonEntityService commonEntityService;\n    @MockBean\n    public WebRpc webRpc;\n    @MockBean\n    public WebRpc2 webRpc2;\n\n    @BeforeEach\n    public void before(){\n        MockitoAnnotations.openMocks(this);\n    }\n\n    @Test\n    public void test() {\n        ApiResult&lt;Void&gt; testSuccess = commonEntityService.test(new CommonEntity());\n        Assert.isTrue(testSuccess.getSuccess(), \"testSuccess fail\");\n        logger.info(\"testSuccess: {}\", JSON.toJSONString(testSuccess));\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u518d\u6267\u884c test() \u65b9\u6cd5\uff0c\u4e0d\u518d\u51fa\u73b0 NoSuchBeanDefinitionException \u5f02\u5e38\uff0c\u4f46\u4f1a\u51fa\u73b0 NullPointerException \u5f02\u5e38\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u867d\u7136\u6ce8\u5165\u4e86 bean\uff0c\u4f46\u662f\u8fd9\u4e2abean\u662f\u4e2a\u7a7a\u7684\uff0c\u56e0\u6b64\u5728 commonEntityService.test \u65b9\u6cd5\u4e2d\u6267\u884c webRpc.get() \u65f6\uff0c\u4f1a\u62a5 NullPointerException \u5f02\u5e38\u3002 \u4e3a\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528 mock\uff0cMockito.when(). thenReturn()\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@ExtendWith(SpringExtension.class)\n@SpringBootTest(classes = CommonTestApplication.class)\npublic class CommonEntityServiceTest {\n\n    private final Logger logger = LoggerFactory.<em>getLogger<\/em>(this.getClass());\n\n    @Autowired\n    private CommonEntityService commonEntityService;\n    @MockBean\n    public WebRpc webRpc;\n    @MockBean\n    public WebRpc2 webRpc2;\n\n    @BeforeEach\n    public void before(){\n        MockitoAnnotations.<em>openMocks<\/em>(this);\n    }\n\n    @Test\n    public void test() {\n        Mockito.<em>when<\/em>(webRpc.get()).thenReturn(ApiResult.<em>success<\/em>(\"mock result 1\"));\n        Mockito.<em>when<\/em>(webRpc.get2(\"test\")).thenReturn(ApiResult.<em>success<\/em>(\"mock result 2\"));\n        ApiResult&lt;Void&gt; testSuccess = commonEntityService.test(new CommonEntity());\n        Assert.<em>isTrue<\/em>(testSuccess.getSuccess(), \"testSuccess fail\");\n        logger.info(\"testSuccess: {}\", JSON.<em>toJSONString<\/em>(testSuccess));\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u518d\u6b21\u6267\u884c test() \u65b9\u6cd5\uff0c\u6b64\u65f6\u6267\u884c\u5df2\u7ecf\u6210\u529f\u4e86\uff0c\u6253\u5370\u65e5\u5fd7\u5982\u4e0b\u6240\u793a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2022-05-21 22:23:23.094  INFO 3760 --- &#91;           main] c.p.j.s.c.c.s.i.CommonEntityServiceImpl  : insert 0 common entity\n2022-05-21 22:23:23.161  INFO 3760 --- &#91;           main] c.p.j.s.c.c.s.CommonEntityServiceTest    : apiResult: {\"code\":\"200\",\"msg\":\"\u8c03\u7528\u6210\u529f\",\"success\":true}<\/code><\/pre>\n\n\n\n<p>\u867d\u7136\u5df2\u7ecf\u6210\u529f\u6267\u884c\u4e86\u5355\u5143\u6d4b\u8bd5\uff0c\u4f46\u5982\u679c\u9700\u8981 mock \u7684 bean \u5f88\u591a\u7684\u8bdd\uff0c\u90a3\u4e0d\u662f\u6bcf\u4e2a\u6d4b\u8bd5\u7c7b\u90fd\u9700\u8981\u5199\u4e00\u904d mock\uff0c\u5f88\u6d6a\u8d39\u65f6\u95f4\u554a\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u9700\u8981 mock \u7684 bean \u5168\u90fd\u653e\u5230\u4e00\u4e2a\u7c7b\u4e2d\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component\npublic class CommonMockFactory {\n\n    @BeforeEach\n    public void before(){\n        MockitoAnnotations.<em>openMocks<\/em>(this);\n    }\n\n    @MockBean\n    public WebRpc webRpc;\n    @MockBean\n    public WebRpc2 webRpc2;\n}\n<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5728\u9700\u8981\u5355\u5143\u6d4b\u8bd5\u7684\u7c7b\u4e2d\u8fdb\u884c\u6ce8\u5165\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@ExtendWith(SpringExtension.class)\n@SpringBootTest(classes = CommonTestApplication.class)\npublic class CommonEntityServiceTest {\n\n    private final Logger logger = LoggerFactory.<em>getLogger<\/em>(this.getClass());\n\n    @Autowired\n    private CommonEntityService commonEntityService;\n    @Autowired\n    private CommonMockFactory commonMockFactory;\n\n    @Test\n    public void test() {\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get()).thenReturn(ApiResult.<em>success<\/em>(\"mock result 1\"));\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get2(\"test\")).thenReturn(ApiResult.<em>success<\/em>(\"mock result 2\"));\n        ApiResult&lt;Void&gt; testSuccess = commonEntityService.test(new CommonEntity());\n        Assert.<em>isTrue<\/em>(testSuccess.getSuccess(), \"testSuccess fail\");\n        logger.info(\"testSuccess: {}\", JSON.<em>toJSONString<\/em>(testSuccess));\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>  <strong>\u27ae<\/strong>  <strong>2-3.\u63d0\u9ad8\u5355\u5143\u6d4b\u8bd5\u8986\u76d6\u7387<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528idea\u81ea\u5e26\u7684\u5355\u5143\u6d4b\u8bd5\u8986\u76d6\u7387\u5de5\u5177\u53ef\u4ee5\u67e5\u770b\u76f8\u5e94\u7684\u8986\u76d6\u7387\u3002 \u7eff\u8272\u7684\u6761\u4ee3\u8868\u5df2\u8986\u76d6\uff0c\u7ea2\u8272\u7684\u6761\u4ee3\u8868\u672a\u8986\u76d6\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/liga-ai-team.feishu.cn\/space\/api\/box\/stream\/download\/asynccode\/?code=MzQ1NWZmN2NkNzAzZjU3NzljYmU5M2UyZDkwZDUwN2Vfc2FLWkdqYVMxTEhIRENoOWZlZXRXa2RobFpKT2llM1lfVG9rZW46Ym94Y25SZHNUY000M1N2ejdIZ2xSbTdJVFVoXzE2NTM1NTQyMzM6MTY1MzU1NzgzM19WNA\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5355\u5143\u6d4b\u8bd5\u7684\u8986\u76d6\u7387\u6587\u6863\uff0c\u5206\u522b\u662f\u7c7b\u8986\u76d6\u7387\u3001\u65b9\u6cd5\u8986\u76d6\u7387\u3001\u884c\u8986\u76d6\u7387\uff0c\u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\u6211\u4eec\u7684\u884c\u8986\u76d6\u7387\u53ea\u670964%\uff0c\u8fd8\u6709\u63d0\u5347\u7684\u7a7a\u95f4\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/liga-ai-team.feishu.cn\/space\/api\/box\/stream\/download\/asynccode\/?code=ZjBkNTkxMmRmZTYzYWI0ZmNmMWQzMjNiYjI4ZDBkN2ZfQlk0ZEdnbUk4ZU55b05ydkQ5MGxWMGVYanBrQnFzTzVfVG9rZW46Ym94Y25UT1l2dEVsaG10RHg3OG81VE9TNDNmXzE2NTM1NTQyMzM6MTY1MzU1NzgzM19WNA\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5982\u4f55\u63d0\u5347\u5462\uff1f\u7b54\u6848\u5c31\u662f mock\u3002 \u5148\u4e0a\u6539\u9020\u540e\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@ExtendWith(SpringExtension.class)\n@SpringBootTest(classes = CommonTestApplication.class)\npublic class CommonEntityServiceTest {\n\n    private final Logger logger = LoggerFactory.<em>getLogger<\/em>(this.getClass());\n\n    @Autowired\n    private CommonEntityService commonEntityService;\n    @Autowired\n    private CommonMockFactory commonMockFactory;\n\n    @Test\n    public void test() {\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get()).thenReturn(ApiResult.<em>success<\/em>(\"mock result 1\"));\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get2(\"test\")).thenReturn(ApiResult.<em>success<\/em>(\"mock result 2\"));\n        ApiResult&lt;Void&gt; testSuccess = commonEntityService.test(new CommonEntity());\n        Assert.<em>isTrue<\/em>(testSuccess.getSuccess(), \"testSuccess fail\");\n        logger.info(\"testSuccess: {}\", JSON.<em>toJSONString<\/em>(testSuccess));\n    }\n\n    @Test\n    public void testWithMock() {\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get()).thenReturn(ApiResult.<em>success<\/em>(\"mock result 1\"));\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get2(\"test\")).thenReturn(ApiResult.<em>success<\/em>(\"mock result 2\"));\n        ApiResult&lt;Void&gt; testSuccess = commonEntityService.test(new CommonEntity());\n        Assert.<em>isTrue<\/em>(testSuccess.getSuccess(), \"testSuccess fail\");\n        logger.info(\"testSuccess: {}\", JSON.<em>toJSONString<\/em>(testSuccess));\n\n        \/\/ \u6a21\u62df webRpc.get() \u5931\u8d25\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get()).thenReturn(ApiResult.<em>error<\/em>(BizRespStatusEnum.<em>ILLEGAL_PARAM<\/em>));\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get2(\"test\")).thenReturn(ApiResult.<em>success<\/em>(\"mock result 2\"));\n        ApiResult&lt;Void&gt; testFail1 = commonEntityService.test(new CommonEntity());\n        Assert.<em>isTrue<\/em>(!testFail1.getSuccess(), \"testFail1 fail\");\n        logger.info(\"testFail1: {}\", JSON.<em>toJSONString<\/em>(testFail1));\n\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get()).thenReturn(ApiResult.<em>success<\/em>(\"mock result 1\"));\n        \/\/ \u6a21\u62df webRpc.get2() \u5931\u8d25\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get2(\"test\")).thenReturn(ApiResult.<em>error<\/em>(BizRespStatusEnum.<em>ILLEGAL_PARAM<\/em>));\n        ApiResult&lt;Void&gt; testFail2 = commonEntityService.test(new CommonEntity());\n        Assert.<em>isTrue<\/em>(!testFail2.getSuccess(), \"testFail1 fail\");\n        logger.info(\"testFail2: {}\", JSON.<em>toJSONString<\/em>(testFail2));\n\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get()).thenReturn(ApiResult.<em>success<\/em>(\"mock result 1\"));\n        Mockito.<em>when<\/em>(commonMockFactory.webRpc.get2(\"test\")).thenReturn(ApiResult.<em>success<\/em>(\"mock result 2\"));\n        try (MockedStatic&lt;RmiUtil&gt; rmiUtilMockedStatic = Mockito.<em>mockStatic<\/em>(RmiUtil.class)) {\n            \/\/ \u6a21\u62df RmiUtil.getRemoteResult() \u5931\u8d25\n            rmiUtilMockedStatic.when(RmiUtil::<em>getRemoteResult<\/em>).thenReturn(Optional.<em>empty<\/em>());\n            ApiResult&lt;Void&gt; testFail3 = commonEntityService.test(new CommonEntity());\n            Assert.<em>isTrue<\/em>(!testFail3.getSuccess(), \"testFail3 fail\");\n            logger.info(\"testFail3: {}\", JSON.<em>toJSONString<\/em>(testFail3));\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u5355\u5143\u6d4b\u8bd5\u7684\u6267\u884c\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2022-05-21 23:23:46.516  INFO 35136 --- &#91;           main] c.p.j.s.c.c.s.i.CommonEntityServiceImpl  : insert 0 common entity\n2022-05-21 23:23:46.589  INFO 35136 --- &#91;           main] c.p.j.s.c.c.s.CommonEntityServiceTest    : testSuccess: {\"code\":\"200\",\"msg\":\"\u8c03\u7528\u6210\u529f\",\"success\":true}\n2022-05-21 23:23:46.590  INFO 35136 --- &#91;           main] c.p.j.s.c.c.s.i.CommonEntityServiceImpl  : getRpc fail: ApiResult{success=false, code='400', msg='\u53c2\u6570\u5f02\u5e38', result=null}\n2022-05-21 23:23:46.590  INFO 35136 --- &#91;           main] c.p.j.s.c.c.s.CommonEntityServiceTest    : testFail1: {\"code\":\"400\",\"msg\":\"\u53c2\u6570\u5f02\u5e38\",\"success\":false}\n2022-05-21 23:23:46.591  INFO 35136 --- &#91;           main] c.p.j.s.c.c.s.i.CommonEntityServiceImpl  : getRpc2 fail: ApiResult{success=false, code='400', msg='\u53c2\u6570\u5f02\u5e38', result=null}\n2022-05-21 23:23:46.591  INFO 35136 --- &#91;           main] c.p.j.s.c.c.s.CommonEntityServiceTest    : testFail2: {\"code\":\"400\",\"msg\":\"\u53c2\u6570\u5f02\u5e38\",\"success\":false}\n2022-05-21 23:23:46.629  INFO 35136 --- &#91;           main] c.p.j.s.c.c.s.i.CommonEntityServiceImpl  : getRemoteResult fail\n2022-05-21 23:23:46.629  INFO 35136 --- &#91;           main] c.p.j.s.c.c.s.CommonEntityServiceTest    : testFail3: {\"code\":\"002\",\"msg\":\"\u7cfb\u7edf\u5f02\u5e38\",\"success\":false}\n<\/code><\/pre>\n\n\n\n<p>\u518d\u6765\u770b\u770b\u6539\u9020\u4e4b\u540e\u7684\u8986\u76d6\u7387\uff01\u4ece\u4e0b\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\u5355\u5143\u6d4b\u8bd5\u7684\u884c\u8986\u76d6\u7387\u8fbe\u5230\u4e86100%\uff0c\u60ca\u4e0d\u60ca\u559c\uff0c\u610f\u4e0d\u610f\u5916\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/liga-ai-team.feishu.cn\/space\/api\/box\/stream\/download\/asynccode\/?code=MWJhY2M3NzJjMmU1ZjNhNjVkNGZkOWE0YjhlNmJhYTZfalI0MGt6WjVFWmVpU0NIbm5DbnhWbmZPN3JFR1Rkc2FfVG9rZW46Ym94Y252VkRWV1BONVJGQlI4Q1FTRGFjUFZmXzE2NTM1NTQyMzM6MTY1MzU1NzgzM19WNA\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/liga-ai-team.feishu.cn\/space\/api\/box\/stream\/download\/asynccode\/?code=MWY0Nzg1MGJhMjgwMzU0OTJjOTJmNjFhZTNkMTI4MzJfUFdJQjM2QnhjdUI2UXl6UnAzTDRHRWVudFA0aTlEYUdfVG9rZW46Ym94Y25XM2lqcjFUUklZN2RjQ0FacWdXMFBmXzE2NTM1NTQyMzM6MTY1MzU1NzgzM19WNA\" alt=\"\"\/><\/figure>\n\n\n\n<h2><strong>3.\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728\u6211\u4eec\u6ca1\u7528 mock \u5de5\u5177\u65f6\uff0c\u522b\u8bf4\u8986\u76d6\u7387\u4e86\uff0c\u6267\u884c\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u90fd\u5f88\u9ebb\u70e6\u3002 \u4f7f\u7528 mock \u5de5\u5177\u4e4b\u540e\uff0c\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u6267\u884c\u5355\u5143\u6d4b\u8bd5\uff0c\u8fd8\u80fd\u4f7f\u7528\u5404\u79cd\u5947\u6280\u6deb\u5de7\u6765\u63d0\u5347\u884c\u8986\u76d6\u7387\uff0c\u5f3a\u70c8\u63a8\u8350\uff01 \u5199\u597d\u5355\u5143\u6d4b\u8bd5\u4e00\u70b9\u90fd\u4e0d\u7b80\u5355\uff0c\u672c\u6587\u53ea\u662f\u62ff\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u573a\u666f\u6765\u4e3e\u4f8b\uff0c\u5728\u5355\u5143\u6d4b\u8bd5\u7684\u884c\u8986\u76d6\u7387\u8fbe\u5230100%\u65f6\uff0c\u4ee3\u7801\u91cf\u5c31\u5df2\u7ecf\u662f\u6e90\u7801\u7684\u4e24\u500d\u8fd8\u591a\u4e86\uff0c\u5bb3\uff01\u4f46\u662f <strong>bug<\/strong> \u548c <strong>\u5355\u5143\u6d4b\u8bd5<\/strong> \u603b\u8981\u9009\u4e00\u4e2a\u7684\uff0c\u770b\u5927\u5bb6\u7684\u9009\u62e9\u4e86\u3002<\/p>\n\n\n\n<p> <strong>\u27ae<\/strong>  \u4e86\u89e3\u66f4\u591a\u654f\u6377\u5f00\u53d1\u3001\u9879\u76ee\u7ba1\u7406\u3001\u884c\u4e1a\u52a8\u6001\u7b49\u6d88\u606f\uff0c\u5173\u6ce8\u6211\u4eec\u7684\u56e2\u961f\u535a\u5ba2\u6216\u70b9\u51fb&nbsp;<a href=\"https:\/\/ligai.cn\/\">LigaAI-\u667a\u80fd\u7814\u53d1\u534f\u4f5c\u5e73\u53f0<\/a>\uff0c\u5728\u7ebf\u7533\u8bf7\u4f53\u9a8c\u6211\u4eec\u7684\u4ea7\u54c1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00\uff1a \u672c\u6587\u9002\u7528\u4e8ejavaer\uff0c\u5176\u4ed6\u5f00\u53d1\u8005\u6216\u8bb8\u53ef\u4ee5\u501f\u9274\u3002 \u5199\u672c\u6587\u7684\u4e3b\u65e8\u6709\u4e24\u4e2a\uff0c\u4e00\u662f\u7b80\u5355\u7684\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e0b\u5355\u5143\u6d4b\u8bd5\uff0c &#8230; <a title=\"\u6280\u672f\u5206\u4eab | Javaer \u5982\u4f55\u505a\u5355\u5143\u6d4b\u8bd5\uff1f\" class=\"read-more\" href=\"https:\/\/ligai.cn\/blog\/team\/985.html\" aria-label=\"\u7ee7\u7eed\u9605\u8bfb\u6280\u672f\u5206\u4eab | Javaer \u5982\u4f55\u505a\u5355\u5143\u6d4b\u8bd5\uff1f\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,22],"tags":[15,46],"_links":{"self":[{"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/posts\/985"}],"collection":[{"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/comments?post=985"}],"version-history":[{"count":2,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/posts\/985\/revisions"}],"predecessor-version":[{"id":991,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/posts\/985\/revisions\/991"}],"wp:attachment":[{"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/media?parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/categories?post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/tags?post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}