![](/img/trans.png)
[英]Mockito/JMockit & Hamcrest matchers : How to verify Lists/Collections?
[英]How do I simplify mockito/hamcrest argument matchers in test method?
以下測試方法出現在彈簧指南中 。 是否有較不復雜的語法來編寫此測試,或者如何將其拆分為較小的塊?
verify(orderService).createOrder(
org.mockito.Matchers.<CreateOrderEvent>argThat(
allOf( org.hamcrest.Matchers.<CreateOrderEvent>
hasProperty("details",
hasProperty("dateTimeOfSubmission", notNullValue())),
org.hamcrest.Matchers.<CreateOrderEvent>hasProperty("details",
hasProperty("name", equalTo(CUSTOMER_NAME))),
org.hamcrest.Matchers.<CreateOrderEvent>hasProperty("details",
hasProperty("address1", equalTo(ADDRESS1))),
org.hamcrest.Matchers.<CreateOrderEvent>hasProperty("details",
hasProperty("postcode", equalTo(POST_CODE)))
)));
您可以切換hasProperty和allOf匹配器。
verify(orderService).createOrder(
org.mockito.Matchers.<CreateOrderEvent>argThat(
org.hamcrest.Matchers.<CreateOrderEvent>hasProperty("details",
allOf(
hasProperty("dateTimeOfSubmission", notNullValue()),
hasProperty("name", equalTo(CUSTOMER_NAME)),
hasProperty("address1", equalTo(ADDRESS1)),
hasProperty("postcode", equalTo(POST_CODE)))
)));
另一種方法是使用參數捕獲器來記錄您要驗證的參數值。
然后,您可以根據需要對值執行斷言。 與使用匹配器相比,這是一種更清晰的方法來驗證參數信息是否正確。
在這個很棒的博客文章中對此進行了更全面的說明:
http://www.planetgeek.ch/2011/11/25/mockito-argumentmatcher-vs-argumentcaptor/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.