gpt4 book ai didi

java - 如何在测试方法中简化 mockito/hamcrest 参数匹配器?

转载 作者:行者123 更新时间:2023-11-29 06:36:39 24 4
gpt4 key购买 nike

下面的测试方法出现在spring-guide tutorial .是否有更简单的语法来编写此测试,或者我如何将其分解成更小的 block ?

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)))
)));

关于java - 如何在测试方法中简化 mockito/hamcrest 参数匹配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19482372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com