{"id":104075,"date":"2020-02-22T22:26:55","date_gmt":"2020-02-22T14:26:55","guid":{"rendered":"http:\/\/4563.org\/?p=104075"},"modified":"2020-02-22T22:26:55","modified_gmt":"2020-02-22T14:26:55","slug":"springboottest-%e6%b5%8b%e8%af%95%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=104075","title":{"rendered":"SpringBootTest \u6d4b\u8bd5\u7684\u4e00\u4e2a\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  SpringBootTest \u6d4b\u8bd5\u7684\u4e00\u4e2a\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : b0644170fc <\/span>  <span><i><\/i> 48<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6211\u7684\u5de5\u7a0b\u662f\u4e2a maven \u7ba1\u7406\u7684 springboot \u9879\u76ee,\u6709\u4e2a\u542f\u52a8\u7c7b\u5982\u4e0b<\/p>\n<pre><code>@EnableApolloConfig @SpringBootApplication @PropertySource @EnableSwagger2 @EnableScheduling @ImportResource({\"classpath:spring\/spring-context.xml\"}) public class CouponApplication extends SpringBootServletInitializer {   public static void main(String[] args) {   SpringApplication.run(CouponApplication.class);  }  } <\/code><\/pre>\n<p>@ImportResource \u5bfc\u5165\u4e86 spring-context.xml \u6587\u4ef6,\u8fd9\u662f\u603b\u7684 xml \u6587\u4ef6,\u91cc\u9762\u5bfc\u5165\u5e74\u5176\u5b83 xml \u6587\u4ef6<\/p>\n<p>\u6211\u73b0\u5728\u60f3\u8981\u6d4b\u8bd5,\u6211\u81ea\u5df1\u53c8\u521b\u5efa\u4e86\u4e00\u4e2a\u542f\u52a8\u7c7b\u5982\u4e0b:<\/p>\n<pre><code>@SpringBootApplication @ImportResource(\"classpath:spring\/spring-datasource.xml\") @ComponentScan(\"...\") @EnableApolloConfig({\"jdbc\", \"config\"}) @Import({BeanConfig.class, DataSourceConfig.class, RedisConfig.class}) @OverrideAutoConfiguration(enabled = false) public class CouponTplServiceImplTestApplication { } <\/code><\/pre>\n<p>@ImportResource \u4e2d\u53ea\u5bfc\u5165\u4e86\u548c\u6570\u636e\u5e93\u76f8\u5173\u7684 xml.<\/p>\n<p>\u4ee5\u4e0b\u662f\u6211\u7684\u6d4b\u8bd5\u7c7b<\/p>\n<pre><code>@SpringBootTest(classes = CouponTplServiceImplTestApplication.class) @RunWith(SpringRunner.class) @MockBean({HttpClientUtils.class, AppInstanceRpc.class}) public class CouponTplServiceImplTest {} <\/code><\/pre>\n<p>\u6211\u5728\u6d4b\u8bd5\u7c7b\u91cc\u5236\u5b9a\u7684\u542f\u52a8\u7c7b\u662f\u6211\u81ea\u5df1\u521b\u5efa\u7684\u542f\u52a8\u7c7b,\u5e76\u4e0d\u662f\u9879\u76ee\u4e2d\u7684.\u56e0\u6b64,\u542f\u52a8\u7c7b\u8981\u52a0\u8f7d\u7684 xml \u6587\u4ef6\u5e94\u8be5\u662f spring\/spring-datasource.xml.\u4f46\u5b9e\u9645\u60c5\u51b5\u5374\u4e0d\u662f.\u542f\u52a8\u6d4b\u8bd5\u7684\u65f6\u5019,\u62a5\u9519\u5982\u4e0b:<\/p>\n<pre><code>Caused by: java.io.FileNotFoundException: class path resource [spring\/spring-context.xml] cannot be opened because it does not exist  at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)  at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)  ... 45 more <\/code><\/pre>\n<p>\u9519\u8bef\u4e2d\u80fd\u770b\u5230 FileNotFoundException: class path resource [spring\/spring-context.xml] . \u8fd9\u662f\u56e0\u4e3a\u6211\u6545\u610f\u628a resouces\/spring\/ \u76ee\u5f55\u4e0b\u7684\u5176\u5b83 xml \u7ed9\u79fb\u9664\u4e86. \u4ece\u7ed3\u679c\u770b,\u597d\u50cf\u662f @SpringBootTest(classes = CouponTplServiceImplTestApplication.class) \u6ca1\u6709\u8d77\u4f5c\u7528. \u5927\u4f6c\u770b\u4e0b\u8fd9\u662f\u4e3a\u4ec0\u4e48?<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"505693\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhazi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u70b9\u8fdb\u6ce8\u89e3\u53bb\u770b\u770b\u6ce8\u91ca\u5c31\u660e\u767d\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"505694\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : b0644170fc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zhazi \u4f60\u662f\u8bf4\u54ea\u4e2a\u6ce8\u89e3? @SpringBootTest \u7684\u6ce8\u89e3\u5417?\u6211\u770b\u4e86, @SpringBootTest(classes = CouponTplServiceImplTestApplication.class)\u7684\u76ee\u7684\u5c31\u662f\u4e3a\u4e86\u52a0\u8f7d\u4e00\u4e2a ApplicationContext \u7684\u5427?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>SpringBootTest \u6d4b\u8bd5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/104075"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=104075"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/104075\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=104075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=104075"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=104075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}