[英]Spring boot does not start when secondary datasource database connection unavailable
[英]How to test that Spring Boot does *not* start if datasource creation failed
有沒有一種方法可以編寫單元測試,以確保如果某個bean創建失敗,Spring Boot API不會啟動。 例如:無法創建數據源bean。
此代碼應為您做到:
@RunWith(SpringRunner.class)
@SpringBootTest
public class AnyAppNameApplicationTests {
@Test
public void contextLoads() {
}
}
從文檔:
可以在運行基於Spring Boot的測試的測試類上指定的注釋。 除了常規的Spring TestContext Framework之外,還提供以下功能:
- 當
@ContextConfiguration(loader=...)
特定的@ContextConfiguration(loader=...)
時,將SpringBootContextLoader用作默認的ContextLoader。- 不使用嵌套
@Configuration
且未指定顯式類時,自動搜索@SpringBootConfiguration
。- 允許使用properties屬性定義自定義環境屬性。
- 提供對不同webEnvironment模式的支持,包括啟動在定義的或隨機端口上偵聽的完全運行的Web服務器的功能。
- 注冊一個TestRestTemplate和/或WebTestClient bean,以在使用完全運行的Web服務器的Web測試中使用。
如果您使用的是JUnit 4,請不要忘記也將
@RunWith(SpringRunner.class)
添加到測試中,否則注釋將被忽略。 如果您使用的是JUnit 5,則無需將等效的@ExtendWith(SpringExtension)
為@SpringBootTest
,而其他@…Test
注釋已經對其進行了注釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.