Java 如何简化测试方法中的mockito/hamcrest参数匹配器?
下面的测试方法出现在a中。 是否有一个不那么复杂的语法来编写这个测试,或者我如何将它分解成更小的块Java 如何简化测试方法中的mockito/hamcrest参数匹配器?,java,spring,unit-testing,mockito,hamcrest,Java,Spring,Unit Testing,Mockito,Hamcrest,下面的测试方法出现在a中。 是否有一个不那么复杂的语法来编写这个测试,或者我如何将它分解成更小的块 verify(orderService).createOrder( org.mockito.Matchers.<CreateOrderEvent>argThat( allOf( org.hamcrest.Matchers.<CreateOrderEvent> hasProperty("details",
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)))
)));
verify(orderService).createOrder(
org.mockito.Matchers.argThat(
allOf(org.hamcrest.Matchers。
hasProperty(“详细信息”,
hasProperty(“dateTimeOfSubmission”,notNullValue()),
org.hamcrest.Matchers.hasProperty(“详细信息”,
hasProperty(“名称”,equalTo(客户名称)),
org.hamcrest.Matchers.hasProperty(“详细信息”,
hasProperty(“地址1”,等于(地址1)),
org.hamcrest.Matchers.hasProperty(“详细信息”,
hasProperty(“邮政编码”,相等(邮政编码)))
)));
另一种方法是使用参数捕获器记录您试图验证的参数值
然后,您可以根据需要对值执行断言。这是一种比使用匹配器更清楚地验证参数信息的方法
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)))
)));
这在这篇伟大的博客文章中有更全面的解释:
另一种方法是使用参数捕获器记录您试图验证的参数值 然后,您可以根据需要对值执行断言。这是一种比使用匹配器更清楚地验证参数信息的方法
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)))
)));
这在这篇伟大的博客文章中有更全面的解释:
您可以切换hasProperty和allOf matchers
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)))
)));
verify(orderService).createOrder(
org.mockito.Matchers.argThat(
org.hamcrest.Matchers.hasProperty(“详细信息”,
全部(
hasProperty(“dateTimeOfSubmission”,notNullValue()),
hasProperty(“名称”,equalTo(客户名称)),
hasProperty(“地址1”,等同于(地址1)),
hasProperty(“邮政编码”,相等(邮政编码)))
)));
您可以切换hasProperty和allOf matchers
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)))
)));
verify(orderService).createOrder(
org.mockito.Matchers.argThat(
org.hamcrest.Matchers.hasProperty(“详细信息”,
全部(
hasProperty(“dateTimeOfSubmission”,notNullValue()),
hasProperty(“名称”,equalTo(客户名称)),
hasProperty(“地址1”,等同于(地址1)),
hasProperty(“邮政编码”,相等(邮政编码)))
)));
是的,当你发布这篇文章时,我的答案写了一半。但这个解决方案比我要写的要好得多+是的,你发这封信的时候,我已经写了一半的答案了。但这个解决方案比我要写的要好得多+1.