62

環境ごとに異なる構成ファイルが必要になるという典型的な問題があるWebアプリケーションがあります。一部の構成はJNDIデータソースとしてアプリケーションサーバーに配置されますが、一部の構成はプロパティファイルに残ります。

したがって、Springプロファイル機能を使用したいと思います。

私の問題は、テストケースが実行されないことです。

context.xml:

<context:property-placeholder 
  location="classpath:META-INF/spring/config_${spring.profiles.active}.properties"/>

テスト:

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

  @Test
  public void testContext(){

  }
}

問題は、プロファイルをロードするための変数が解決されていないことのようです。

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

現在のプロファイルは@ActiveProfile注釈付きで設定する必要があります。テストケースなので、は使用できませんweb.xml。可能であれば、ランタイムオプションも避けたいと思います。テストはそのまま実行する必要があります(可能な場合)。

プロファイルを適切にアクティブ化するにはどうすればよいですか?context.xmlでプロファイルを設定することは可能ですか?実際に通常のコンテキストを呼び出しているtest-context.xmlで変数を宣言できますか?

4

5 に答える 5

64

この方法でテストを定義することをお勧めしますか?

@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{

  }
}

myprops.propertiesファイルに次の内容が含まれています。

spring.profiles.active=localtest

これにより、2番目のプロパティファイルが解決されるはずです。

META-INF/spring/config_${spring.profiles.active}.properties
于 2012-11-13T16:24:43.823 に答える
7

Bijuの答えを見て、私は実用的な解決策を見つけました。

追加のコンテキストファイルを作成しましたtest-context.xml

<context:property-placeholder location="classpath:config/spring-test.properties"/>

プロファイルを含む:

spring.profiles.active=localtest

そして、テストをロードします:

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

  @Test
  public void testContext(){

  }
}

これにより、複数のテストケースを作成する際の作業が節約されます。

于 2012-11-13T16:39:45.167 に答える
1

ここでの最善のアプローチは、@ ActiveProfilesアノテーションを削除し、次のことを行うことです。

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

  @BeforeClass
  public static void setSystemProperty() {
        Properties properties = System.getProperties();
        properties.setProperty("spring.profiles.active", "localtest");
  }

  @AfterClass
  public static void unsetSystemProperty() {
        System.clearProperty("spring.profiles.active");
  }

  @Test
  public void testContext(){

  }
}

また、test-context.xmlには次のものが含まれている必要があります。

<context:property-placeholder 
  location="classpath:META-INF/spring/config_${spring.profiles.active}.properties"/>
于 2015-06-02T07:27:24.510 に答える
1
public class LoginTest extends BaseTest {
    @Test
    public void exampleTest( ){ 
        // Test
    }
}

基本テストクラスから継承します(この例はtestngではjUnitありませんActiveProfilesが、同じです):

@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
@ActiveProfiles(resolver = MyActiveProfileResolver.class)
public class BaseTest extends AbstractTestNGSpringContextTests { }

MyActiveProfileResolver使用するプロファイルを決定するために必要なロジックを含めることができます。

public class MyActiveProfileResolver implements ActiveProfilesResolver {
    @Override
    public String[] resolve(Class<?> aClass) {
        // This can contain any custom logic to determine which profiles to use
        return new String[] { "exampleProfile" };
    }
}

これにより、テストに必要な依存関係を解決するために使用されるプロファイルが設定されます。

于 2018-04-27T10:15:43.440 に答える
0

@EnableConfigurationPropertiesが存在する必要があり(テストクラスにアノテーションを付けることもできます)、test/resourcesのapplication-localtest.ymlがロードされます。jUnit5のサンプル

@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties
@ContextConfiguration(classes = {YourClasses}, initializers = ConfigFileApplicationContextInitializer.class)
@ActiveProfiles(profiles = "localtest")
class TestActiveProfile {

    @Test
    void testActiveProfile(){

    }
}
于 2020-07-09T04:28:24.743 に答える