嗨,我有一个简单的应用程序,我尝试编写一些集成测试。所以我在我的 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,但它确实如此。
为什么会这样?我找不到关于这个问题的任何线索......