{"id":6873,"date":"2022-08-04T15:31:35","date_gmt":"2022-08-04T07:31:35","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6873"},"modified":"2022-08-04T15:31:35","modified_gmt":"2022-08-04T07:31:35","slug":"spring%e7%9a%84reflectiontestutils","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6873","title":{"rendered":"Spring\u7684ReflectionTestUtils"},"content":{"rendered":"\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>ReflectionUtils\u662fSpring\u4e2d\u4e00\u4e2a\u5e38\u7528\u7684\u7c7b\uff0c\u5c5e\u4e8espring-core\u5305\uff1bReflectionTestUtils\u5219\u5c5e\u4e8espring-test\u5305\u3002\u4e24\u8005\u529f\u80fd\u6709\u91cd\u53e0\u7684\u5730\u65b9\uff0c\u800cReflectionUtils\u4f1a\u66f4\u5f3a\u5927\u3002\u5728\u5355\u5143\u6d4b\u8bd5\u65f6\u4f7f\u7528ReflectionTestUtils\uff0c\u80fd\u589e\u52a0\u6211\u4eec\u7684\u4fbf\u5229\u6027\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u4f46\u5b83\u7684\u67d0\u4e9b\u6210\u5458\u53d8\u91cf\u662f\u79c1\u6709\u7684\uff0c\u5e76\u4e14\u6ca1\u6709\u63d0\u4f9b\u516c\u5171\u7684setter\u65b9\u6cd5\uff0c\u800c\u6211\u4eec\u65e0\u6cd5\u6309\u7167\u5b83\u7684\u6b63\u5e38\u521d\u59cb\u5316\u6d41\u7a0b\u6765\u4f7f\u5b83\u7684\u8fd9\u4e9b\u6210\u5458\u53d8\u91cf\u662f\u6211\u4eec\u60f3\u8981\u7684\u503c\u3002\u8fd9\u65f6\u5c31\u9700\u8981\u60f3\u529e\u6cd5\u4fee\u6539\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u53d8\u91cf\uff0c\u800c\u53cd\u5c04\u53ef\u4ee5\u5e2e\u52a9\u5230\u6211\u4eec\u3002Spring\u4e5f\u63d0\u4f9b\u4e86\u53cd\u5c04\u76f8\u5173\u7684\u5de5\u5177\u7c7b\uff1aReflectionUtils\u548cReflectionTestUtils\uff0c\u8fd9\u91cc\u53ea\u4ecb\u7ecdReflectionTestUtils\u7684\u5e38\u7528\u529f\u80fd\u3002<\/p>\n\n\n\n<p><strong>ReflectionTestUtils\u4e3b\u8981\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u83b7\u53d6\u5bf9\u8c61\u7684\u6210\u5458\u53d8\u91cf\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 static Object getField(@Nullable Object targetObject, String name)<\/pre>\n\n\n\n<p>\u7ed9\u5bf9\u8c61\u6ce8\u5165\u6210\u5458\u53d8\u91cf\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 static void setField(Class&lt;?> targetClass, String name, @Nullable Object value)<\/pre>\n\n\n\n<p>\u8c03\u7528\u6210\u5458\u65b9\u6cd5\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 static &lt;T> T invokeMethod(Object target, String name, Object... args)<\/pre>\n\n\n\n<p><strong>\u4f7f\u7528\u4f8b\u5b50\uff1a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5f53\u4f7f\u7528<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=junit&amp;spm=1001.2101.3001.7020\" target=\"_blank\">junit<\/a>\u6765\u6d4b\u8bd5Spring\u7684\u4ee3\u7801\u65f6\uff0c\u4e3a\u4e86\u51cf\u5c11\u4f9d\u8d56\uff0c\u9700\u8981\u7ed9\u5bf9\u8c61\u7684\u4f9d\u8d56\uff0c\u8bbe\u7f6e\u4e00\u4e2amock\u5bf9\u8c61\uff0c\u4f46\u662f\u7531\u4e8eSpring\u53ef\u4ee5\u4f7f\u7528@Autoware\u7c7b\u4f3c\u7684\u6ce8\u89e3\u65b9\u5f0f\uff0c\u5bf9\u79c1\u6709\u7684\u6210\u5458\u8fdb\u884c\u8d4b\u503c\uff0c\u6b64\u65f6\u65e0\u6cd5\u76f4\u63a5\u5bf9\u79c1\u6709\u7684\u4f9d\u8d56\u8bbe\u7f6emock\u5bf9\u8c61\u3002\u53ef\u4ee5\u901a\u8fc7\u5f15\u5165ReflectionTestUtils\uff0c\u89e3\u51b3\u4f9d\u8d56\u6ce8\u5165\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u5728Spring<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E6%A1%86%E6%9E%B6&amp;spm=1001.2101.3001.7020\" target=\"_blank\">\u6846\u67b6<\/a>\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6ce8\u89e3\u7684\u65b9\u5f0f\u5982\uff1a@Autowair\u3001@Inject\u3001@Resource\uff0c\u5bf9\u79c1\u6709\u7684\u65b9\u6cd5\u6216\u5c5e\u6027\u8fdb\u884c\u6ce8\u89e3\u8d4b\u503c\uff0c\u5982\u679c\u9700\u8981\u4fee\u6539\u8d4b\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528ReflectionTestUtils\u8fbe\u5230\u76ee\u7684\u3002<\/p>\n\n\n\n<p>\u5f85\u6d4b\u8bd5\u7c7b:Foo<\/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=\"\">package com.github.yongzhizhan.draftbox.springtest;\n \nimport org.springframework.beans.factory.annotation.Autowired;\n \nimport javax.annotation.PostConstruct;\nimport javax.annotation.PreDestroy;\n \n\/**\n * \u88ab\u6d4b\u7c7b\n *\/\n@Service\npublic class Foo {\n    @Autowired\n    private String m_String;\n \n    @PostConstruct\n    private void onStarted(){\n        System.out.println(\"on started \" + m_String);\n    }\n \n    @PreDestroy\n    private void onStop(){\n        System.out.println(\"on stop \" + m_String);\n    }\n}<\/pre>\n\n\n\n<p>\u4f7f\u7528ReflectionTestUtils\u89e3\u51b3<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5&amp;spm=1001.2101.3001.7020\" target=\"_blank\">\u4f9d\u8d56\u6ce8\u5165<\/a>\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=\"\">package com.github.yongzhizhan.draftbox.springtest;\n \nimport org.junit.Test;\nimport org.springframework.test.util.ReflectionTestUtils;\n \n\/**\n * \u4f7f\u7528ReflectionTestUtils\u89e3\u51b3\u4f9d\u8d56\u6ce8\u5165\n * @author \n *\/\npublic class ReflectionTestUtilsTest {\n\n    @Autowired\n    private Foo tFoo;\n\n    @Test\n    public void testDefault(){\n \n        \/\/set private property\n        ReflectionTestUtils.setField(tFoo, \"m_String\", \"Hello\");\n \n        \/\/invoke construct and destroy method\n        ReflectionTestUtils.invokeMethod(tFoo, \"onStarted\");\n        ReflectionTestUtils.invokeMethod(tFoo, \"onStop\");\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb ReflectionUtils\u662fSpring\u4e2d\u4e00\u4e2a\u5e38\u7528\u7684\u7c7b\uff0c\u5c5e\u4e8espring-core\u5305\uff1bReflect [&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\/6873"}],"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=6873"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6873\/revisions"}],"predecessor-version":[{"id":6874,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6873\/revisions\/6874"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}