2017-03-13 48 views
0

我正在從junit4過渡到junit5項目,並試圖找出如何測試日誌。以前,我用如何測試junit5中的日誌?

@Rule 
OutputCapture outputCapture = new OutputCapture(); 

,然後將寫使用outputCapture.toString()斷言,例如

assertThat(outputCapture.toString(),containsString("status=200"));

由於@Rule註釋但在junit5一直沒有執行,我不能使用outputCapture。任何想法,而不是做什麼?謝謝!

回答

1

Migration TipJUnit5 documentation明確指出 -

@Rule@ClassRule不復存在;取代@ExtendWith;有關部分規則支持,請參閱以下部分。

爲了使用existing @Rule support from JUnit 4,有一種方法或類級別註釋的建議方法。

與在JUnit 4中一樣,支持規則註釋字段以及方法。 通過在測試類上使用這些類級別的擴展,例如舊規則代碼庫中的規則 實現可以保持不變,其中包括JUnit 4規則導入語句的 。

這有限的規則支持的形式可以通過 類級別的註解 org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport


一個更好的選擇仍然會被重新設計你的測試套件使用Extension Model from JUnit5如果你接通使用它。

相關問題