spring boot test加载所有配置文件

时间:2017-05-19 15:05:16

标签: java spring spring-profiles spring-boot-test

您好我有一个简单的应用程序,我尝试编写一些集成测试。所以我在我的JUnit测试中使用springboot,我需要稍微不同的配置来进行测试开发和生产,所以我使用配置文件。我现在的问题是,如果我将我的应用程序部署到独立的tomcat服务器,那么配置文件工作正常。但是当使用springboot启动JUnit测试时,应用程序会尝试加载导致冲突的所有配置文件配置

“没有类型'org.springframework.context.MessageSource'的限定bean可用:预期的单个匹配bean但找到2”

正如我已经说过,这适用于独立的tomcat。

这是我的代码片段:

这是我的测试类

@ActiveProfiles(ProfileConstants.TEST)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment =     SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {SpringBootInitializer.class})
public class MyTest

然后是SpringBootInitializer

@Configuration
@EnableAutoConfiguration
@Import(WebAppConfig.class)
public class SpringBootInitializer extends SpringBootServletInitializer implements ServletContextAware
{

  /**
   * this method will force the embedded tomcat to load the jsf-configuration
   * 
   * @param servletContext the servletcontext of embedded tomcat
   */
  @Override
  public void setServletContext(ServletContext servletContext)
  {
    // sets the spring default initialization profile to test
    servletContext.setInitParameter("spring.profiles.active", ProfileConstants.TEST);
    // will ensure, that the context-initialization is executed. If not set to true the jsf-config will not be
    // loaded
    servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", "true");
    // will initialize jsf-context
    servletContext.addListener(com.sun.faces.config.ConfigureListener.class);
  }

  /**
   * registration of the {@link FacesServlet} with url mappings to .xhtml, .jsf and .faces
   * 
   * @return the ServletRegistrationBean with registered {@link FacesServlet}
   */
  @Bean
  public ServletRegistrationBean facesServletRegistration()
  {
    ServletRegistrationBean registration = new ServletRegistrationBean(new FacesServlet(), "*.xhtml", "*.jsf",
                                                                       "*.faces");
    registration.setLoadOnStartup(1);
    return registration;
  }
}

最后我的WebAppConfig.class

@Configuration
@ComponentScan({"my.component.package"})
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter
{
    ...
    /**
     * spring locale. the resource-bundles will be set here . <br>
     * also the {@link MessageReaderHolder} will be initialized here that holds a static default implementation
     * to read the messages from the resource bundles.
     *
     * @return the resource bundles
     */
    @Profile({ ProfileConstants.PRODUCTION, ProfileConstants.TEST})
    @Bean(name = "messageSource")
    public MessageSource messageSourceProduction()
    {
      ReloadableResourceBundleMessageSource bundleMessageSource = new ReloadableResourceBundleMessageSource();
      bundleMessageSource.setBasenames("classpath:locale");
      MessageReaderHolder.initialize(bundleMessageSource);
      return bundleMessageSource;
    }

    /**
     * spring locale. the resource-bundles will be set here . <br>
     * also the {@link MessageReaderHolder} will be initialized here that holds a static default implementation
     * to read the messages from the resource bundles.
     *
     * @return the resource bundles
     */
    @Profile({ ProfileConstants.DEVELOPMENT})
    @Bean(name = "messageSource")
    public MessageSource messageSourceDevelopment()
    {
      ReloadableResourceBundleMessageSource bundleMessageSource = new ReloadableResourceBundleMessageSource();
      bundleMessageSource.setBasenames("classpath:locale");
      bundleMessageSource.setCacheSeconds(30); 
      MessageReaderHolder.initialize(bundleMessageSource);
      return bundleMessageSource;
    }
}

我希望只根据我使用的配置文件将一个messageSource加载到配置中。由于我总是只使用1个配置文件,因此应用程序不应该加载重复的bean,但确实如此。

为什么会这样? 我找不到任何关于这个问题的线索......

0 个答案:

没有答案