2008-11-07 89 views

回答

106

當然,您可以:

assert x > 0 : "x must be greater than zero, but x = " + x; 

更多信息請參見Programming with Assertions

10

它絕對不會:

assert importantVar != null : "The important var was null!"; 

這將增加「最重要的VAR爲空」到被拋出的異常。

6

如果使用

assert Expression1 : Expression2 ; 

表達式2被用作AssertionError的詳細消息。

+0

如果Expression1爲false,只需添加AssertionError即可。 – 2013-12-12 16:31:29

14
assert (condition) : "some message"; 

我建議你把條件括號

assert (y > x): "y is too small. y = " + y; 

試想一下,如果你遇到像這樣的代碼來了......

assert isTrue() ? true : false : "some message"; 

不要忘記這一點無關斷言你會用JUnit寫。

相關問題