1

Spring Boot 1.3.0.RELEASE の新機能を使用しようとしています- 次の構成を介してプロファイルをアクティブ化しますspring-boot-maven-plugin

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <profiles>
            <profile>test</profile>
        </profiles>
    </configuration>
    <executions>
        <execution>
            <id>start-application</id>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-application</id>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

しかし、この場合、私の統合テストは失敗し始めます。IllegalArgumentException: Could not resolve placeholder 'spring.mail.host' in string value "${spring.mail.host}"

で定義されたこの変数src/main/resources/application-test.properties:

spring.profiles: test
spring.mail.host: 127.0.0.1

私のテストは次のようになります。

@ContextConfiguration(
    loader = AnnotationConfigContextLoader.class,
    initializers = ConfigFileApplicationContextInitializer.class,
    classes = TestContext.class
)
public class WhenAnonymousUserRegisterAccount extends AbstractTestNGSpringContextTests {

    @Value("${spring.mail.host}")
    private String mailHost;

TestContextPropertySourcesPlaceholderConfigurer別のプロパティ ファイルでのみ定義します。

興味深いのは、テスト<profiles>からプロファイルを削除して有効にすると、次のように動作することです。application.properties

spring.profiles.active: test

そのため、Spring を使用していると、ファイルが環境に<profiles>ロードされないように見えます。application-test.properties

質問:

  • バグですか?
  • (そうでない場合)ロードapplication-test.propertiesして使用するようにSpringを構成する方法は<profiles>
  • これらのアプローチが異なるのはなぜですか?
4

1 に答える 1

2

の構成でプロファイルを指定するspring-boot-maven-pluginと、そのプラグインを使用してアプリケーションを実行する場合、つまり を実行する場合にのみ使用できますmvn spring-boot:run。あなたの統合テストには当てはまらないので、うまくいきません。

于 2016-01-08T22:31:59.207 に答える