私はスプリングブート1.4.3プロジェクトを持っています。 test/resources
フォルダに2つのプロパティファイルがあります。 a-test.properties
とb-test.properties
としましょう。スプリングブートは、@TestPropertySourceアノテーションに関係なく利用可能なすべてのプロパティファイルをロードします
次のようにテストクラスが注釈されている:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:a-test.properties")
しかし、私はb-test.properties
からもプロパティが(私はシンプルなプリント出力を介してこれを検証)にロードされている私のテストでご覧ください。
なぜですか?どうすればこれを防ぐことができますか?
a-test.properties
は私のテスト
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:base-test.properties", inheritProperties=false)
public class EmailServiceContextBasedTest {
@SpyBean
public JavaMailSender javaMailSender;
@Before
public void setUp() throws Exception {
System.out.println(
((JavaMailSenderImpl)javaMailSender).getPassword()
);
System.out.println(
((JavaMailSenderImpl)javaMailSender).getJavaMailProperties()
);
}
@Test
public void test() throws Exception {
// do nothing
}
}
から抽出された例:SpringBootTest
と
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.starttls.enable=false
とb-test.properties
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=myPassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
どのようにプロパティを読んでいますか?記述している動作を示すコードを追加できますか? – dimitrisli
@dimitrisliええ、確か!私は例を添付しました –
@mat_boyあなたが得る出力と期待する出力を提供してください。あなたの例では、プロパティファイルの1つ(私の場合は 'a-test.properties')に関連付けられたパスワードだけが出力されます。では、b-test.propertiesのプロパティもロードされていることを、どのように確認しましたか? – Omid