、私は例えば、すべてのgitのコミット情報を含む、git.properties
という名前のファイルを生成するgit-commit-id-plugin
Mavenプラグインを使用します。Springブート統合テストで生成されたプロパティファイルを読み取る方法は?
git.commit.id=35ca97298544d4ee6f8a5392211ebaa0d9bdafeb
このファイルはtarget/classes
リポジトリに直接生成されます。それはクラスパスに含まれています。実行時にファイルが私のメインアプリケーションクラスにアノテーションを介してロードされます。
@PropertySource({"git.properties"})
私はその後、git.properties
ファイルに含まれるプロパティの値を取得するために私の豆で式を使用することができます。
@Value("${git.commit.id}")
private String gitCommitIdFull; // will contain "35ca97298544d4ee6f8a5392211ebaa0d9bdafeb"
アプリを正常に実行すると、すべてうまく動作します。
しかし、私は今で実行されるいくつかの統合テストを実行しようとしています:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleSearchDAOTest {
//tests here...
}
私は次の例外を取得:明らか
java.lang.IllegalStateException: Failed to load ApplicationContext
(...)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to parse configuration class [ch.cscf.mds.MdsApiApplication];
nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/git.properties]
、テストが実行されている方法は、彼らがいるようですクラスパスの基底としてtarget/classes
を使用しないでください。
何を使用しますか?これらのテストのランタイムをtarget/classes/git.properties
ファイルに認識させるにはどうすればよいですか?
target/classes
リポジトリではなくsrc/main/resources
ディレクトリにgit.properties
ファイルを生成しようとしました。私はまだ同じエラーがあります。
'@PropertySource({" classpath:git.properties "})'おそらく。 – Vaelyr
優秀!できます。私はそれを受け入れることができるように答えることができますか? –
そしてそれがなぜうまくいくのかについてのいくつかの説明もあります:) –