簡體   English   中英

如果數據源創建失敗,如何測試Spring Boot不會啟動

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM