{"id":4978,"date":"2020-04-18T19:02:34","date_gmt":"2020-04-18T11:02:34","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4978"},"modified":"2020-04-18T19:02:34","modified_gmt":"2020-04-18T11:02:34","slug":"%e8%af%b4%e4%b8%80%e8%af%b4springboottest-webenvironment","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4978","title":{"rendered":"\u8bf4\u4e00\u8bf4SpringBootTest.WebEnvironment"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5148\u8bf4\u4e00\u8bf4\u5b66\u4e60\u65b9\u6cd5\u3002\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9047\u5230\u95ee\u9898\u4e86\u90fd\u4f1a\u5148\u53bb\u767e\u5ea6\uff0c\u4f46\u662f\u767e\u5ea6\u51fa\u6765\u7684\u6587\u7ae0\u4e0d\u4e00\u5b9a\u5168\u5bf9\uff0c\u800c\u4e14\u6280\u672f\uff0c\u8d28\u91cf\u53c2\u5dee\u4e0d\u9f50\u3002\u8fd9\u4e2a\u65f6\u5019\u6700\u597d\u7684\u529e\u6cd5\u662f\u53bb\u67e5\u770b\u5b98\u65b9\u6587\u6863\uff0c\u5f53\u7136\u6211\u4eec\u4e5f\u5b8c\u5168\u4e0d\u80fd\u4fdd\u8bc1\u5b98\u65b9\u7684\u6587\u6863\u5c31\u4e00\u5b9a\u6b63\u786e\u3002\u4f46\u6211\u4eec\u8fd8\u662f\u5e94\u8be5\u76f8\u4fe1\u5b98\u65b9\u6587\u6863\uff0c\u7136\u540e\u7ed3\u5408\u4e0a\u4e0b\u6587\u6765\u786e\u5b9a\u6b63\u786e\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4eca\u5929\u5468\u516d\uff0c\u6211\u5728\u5e26\u5a03\uff0c\u4e0d\u80fd\u517c\u987e\u5230\u5927\u5bb6\uff0c\u8bf7\u4e92\u76f8\u8c05\u89e3\u3002\u5e73\u65f6\u4e5f\u6709\u4e0d\u5c11\u7f51\u53cb\u79c1\u4fe1\u6211\uff0c\u95ee\u4e00\u4e9b\u6280\u672f\u95ee\u9898\uff0c\u4f46\u6211\u4e2a\u4eba\u7684\u65f6\u95f4\u548c\u7cbe\u529b\u90fd\u975e\u5e38\u7684\u6709\u9650\u3002\u5e73\u65f6\u591a\u79ef\u7d2f\u77e5\u8bc6\uff0c\u9047\u5230\u95ee\u9898\u624d\u80fd\u6e38\u5203\u6709\u4f59\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u62bd\u4e2a\u65f6\u95f4\uff0c\u6211\u6765\u8bf4\u8bf4\u6807\u9898\u4e2d\u7684 SpringBootTest.WebEnvironment\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u5b57\u9762\u610f\u601d\u5c31\u5e94\u8be5\u731c\u51fa\u4e2a\u5927\u534a\uff0c\u5b83\u662f SpringBoot \u7684\u6d4b\u8bd5\u65f6\uff0c\u7528\u6765\u6307\u5b9a web \u73af\u5883\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SpringBootTest.WebEnvironment \u4e2d\u4e00\u5171\u6709 4 \u4e2a\u679a\u4e3e\u503c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>DEFINED_PORT\uff1a\u521b\u5efa\u4e00\u4e2a\uff08reactive\uff09 web \u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\uff0c\u4f7f\u7528\u9ed8\u8ba4\u7aef\u53e3\u3002\u9488\u5bf9\u8fd9\u4e2a\u5c5e\u6027\uff0c\u7f51\u4e0a\u7ffb\u8bd1\u7684\u89e3\u8bfb\u6bd4\u8f83\u591a\uff0c\u9519\u8bef\u7684\u4e5f\u591a\u3002<\/li><li>MOCK\uff1aMOCK \u662f\u7528\u7684\u6bd4\u8f83\u591a\u7684\u4e00\u4e2a\u5c5e\u6027\u3002\u5b83\u4f1a\u5224\u65ad\uff0c\u5f53\u4f60\u4f7f\u7528 servlet API \u65f6\uff0c\u5219\u4f7f\u7528\u6a21\u62df servlet \u73af\u5883\u521b\u5efa WebApplicationContext\uff1b\u5982\u679c\u662f\u4f7f\u7528  WebFlux\uff0c\u5219\u4f7f\u7528 ReactiveWebApplicationContext\uff1b\u5426\u5219\uff0c\u4f7f\u7528\u5e38\u89c4\u7684 ApplicationContext\u3002<\/li><li>NONE\uff1a\u8fd9\u4e2a\u4e5f\u6709\u5f88\u591a\u4eba\u7406\u89e3\u9519\u8bef\uff0c\u5176\u5b9e\u5b83\u4e3b\u8981\u662f\u8868\u793a\uff0c\u5b83\u4e0d\u4f1a\u6307\u5b9a SpringApplication.setWebApplicationType\u3002\u4ee5\u975e Web \u73af\u5883\u6765\u8fd0\u884c\u3002\u4e5f\u5c31\u662f\u975e Servlet \u548c Reactive \u73af\u5883\u3002<\/li><li>RANDOM_PORT\uff1a\u521b\u5efa\u4e00\u4e2a web \u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\uff0c\u53ef\u4ee5\u662f reactive\uff0c\u4e5f\u53ef\u4ee5\u662f servlet\u3002\u5b83\u5c06\u968f\u673a\u8d77\u4e00\u4e2a\u7aef\u53e3\u5e76\u76d1\u542c\u3002\u5230\u5e95\u662f reactive \u8fd8\u662f Servlet\uff0c\u5b83\u4f1a\u81ea\u5df1\u6839\u636e\u4e0a\u4e0b\u6587\u6765\u5224\u65ad\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e9b\u4eba\u8ba4\u4e3a\uff0cMock \u548c NONE \u7684\u4f5c\u7528\u91cd\u590d\u4e86\uff0c\u5176\u5b9e\u5e76\u6ca1\u6709\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">WebApplicationType \u7684\u7c7b\u578b\u6709 3 \u79cd\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>WebApplicationType.REACTIVE\uff1aWeb Reactive<\/li><li>WebApplicationType.SERVLET\uff1aWeb Servlet<\/li><li>WebApplicationType.NONE\uff1a\u975e Web<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>static WebApplicationType deduceFromClasspath() {\n\tif (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null)\n\t\t\t&amp;&amp; !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)\n\t\t\t&amp;&amp; !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {\n\t\treturn WebApplicationType.REACTIVE;\n\t}\n\tfor (String className : SERVLET_INDICATOR_CLASSES) {\n\t\tif (!ClassUtils.isPresent(className, null)) {\n\t\t\treturn WebApplicationType.NONE;\n\t\t}\n\t}\n\treturn WebApplicationType.SERVLET;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u8bf4\u4e00\u70b9\uff0c\u8fd8\u6709\u4e0d\u5c11\u4eba\u7684\u6587\u7ae0\u8bf4\uff0cSpringBootTest.WebEnvironment.RANDOM_PORT \u5fc5\u987b\u6216\u901a\u5e38\u548c @LocalServerPort \u4e00\u8d77\u4f7f\u7528\u3002\u5b98\u65b9\u6587\u6863\u5e76\u6ca1\u6709\u8fd9\u4e48\u8bf4\uff0c\u6211\u672c\u4eba\u5c31\u5f88\u5c11\u4f7f\u7528 @LocalServerPort\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4978\">\u8bf4\u4e00\u8bf4SpringBootTest.WebEnvironment<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5148\u8bf4\u4e00\u8bf4\u5b66\u4e60\u65b9\u6cd5\u3002\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9047\u5230\u95ee\u9898\u4e86\u90fd\u4f1a\u5148\u53bb\u767e\u5ea6\uff0c\u4f46\u662f\u767e\u5ea6\u51fa\u6765\u7684\u6587\u7ae0\u4e0d\u4e00\u5b9a\u5168\u5bf9\uff0c\u800c\u4e14\u6280\u672f\uff0c\u8d28\u91cf\u53c2\u5dee\u4e0d\u9f50\u3002 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[587,588,1350,1522,590],"class_list":["post-4978","post","type-post","status-publish","format-standard","hentry","category-java","tag-spring-boot","tag-spring-cloud","tag-spring-mvc","tag-springboot","tag-springboottest"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4978"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4978\/revisions"}],"predecessor-version":[{"id":4979,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4978\/revisions\/4979"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}