![](/img/trans.png)
[英]How to use hamcrest nullValue with type inference in assertThat
[英]How to use Hamcrest's AssertThat for String[]
所以我一直在四處尋找解決這個問題的方法,但是我遇到了編譯器錯誤或怪異的期望,或兩者兼而有之。 所以我們開始:
this.mockPersonNode.setProperty("fname",new String[] {"John"});
...unrelated code...
//validate traits
final String[] fname = (String[]) groovy.getProperty("firstName");
//This is where my problems lie
assertThat(fname, hasProperty("John"));
因此,此代碼可以正常編譯,但是當我在Maven中進行構建時,測試失敗,原因是: Expected: hasProperty("John"), got:[John]
所以我做了一些檢查,並檢查了其他人在這里回答的問題,但是我遇到了編譯錯誤,我顯然在做assertThat錯誤,但是應該如何設置assertThat?
使用hasItemInArray
匹配器:
assertThat(fname, hasItemInArray("John"));
hasProperty
匹配器匹配Java Bean屬性。
如果要斷言陣列fname
包含該項目John
並沒有別的你可以使用IsArrayContainingInOrder匹配( Matchers.arrayContaining
):
assertThat(fname, arrayContaining("John"));
如果你只關心在至少一個項目fname
是John
使用IsArrayContaining匹配( Matchers.hasItemInArray
由@hzpz建議)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.