弹簧轮廓和测试

Udo*_*eld 43 java profile junit spring

我有一个Web应用程序,我有一个典型的问题,它需要不同环境的不同配置文件.某些配置作为JNDI数据源放置在应用程序服务器中,但某些配置保留在属性文件中.

因此,我想使用Spring配置文件功能.

我的问题是我没有运行测试用例.

context.xml中:

<context:property-placeholder 
  location="classpath:META-INF/spring/config_${spring.profiles.active}.properties"/>
Run Code Online (Sandbox Code Playgroud)

测试用例:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
    TestPreperationExecutionListener.class
    })
@Transactional
@ActiveProfiles(profiles = "localtest")
@ContextConfiguration(locations = {
    "classpath:context.xml" })
public class TestContext {

  @Test
  public void testContext(){

  }
}
Run Code Online (Sandbox Code Playgroud)

问题似乎是加载配置文件的变量未解决:

Caused by: java.io.FileNotFoundException: class path resource [META-INF/spring/config_${spring.profiles.active}.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:138)
... 31 more
Run Code Online (Sandbox Code Playgroud)

应使用@ActiveProfile注释设置当前配置文件.因为它是一个测试用例,我将无法使用web.xml.如果可能的话,我也想避免运行时选项.测试应按原样运行(如果可能).

如何正确激活配置文件?是否可以使用context.xml设置配置文件?我可以在test-context.xml中声明实际调用普通上下文的变量吗?

Bij*_*men 45

我可以推荐这样做,定义你的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
    TestPreperationExecutionListener.class
    })
@Transactional
@ActiveProfiles(profiles = "localtest")
@ContextConfiguration
public class TestContext {

  @Test
  public void testContext(){

  }

  @Configuration
  @PropertySource("classpath:/myprops.properties")
  @ImportResource({"classpath:context.xml" })
  public static class MyContextConfiguration{

  }
}
Run Code Online (Sandbox Code Playgroud)

以及myprops.properties文件中的以下内容:

spring.profiles.active=localtest
Run Code Online (Sandbox Code Playgroud)

有了这个你的第二个属性文件应该得到解决:

META-INF/spring/config_${spring.profiles.active}.properties
Run Code Online (Sandbox Code Playgroud)

  • 这种方法并不干净,您必须转到应更改活动配置文件值的属性文件。 (2认同)

Udo*_*eld 6

看着Biju的回答,我找到了一个有效的解决方案.

我创建了一个额外的上下文文件test-context.xml:

<context:property-placeholder location="classpath:config/spring-test.properties"/>
Run Code Online (Sandbox Code Playgroud)

包含个人资料:

spring.profiles.active=localtest
Run Code Online (Sandbox Code Playgroud)

并加载测试:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
    TestPreperationExecutionListener.class
    })
@Transactional
@ActiveProfiles(profiles = "localtest")
@ContextConfiguration(locations = {
    "classpath:config/test-context.xml" })
public class TestContext {

  @Test
  public void testContext(){

  }
}
Run Code Online (Sandbox Code Playgroud)

这可以在创建多个测试用例时节省一些工作.