Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何简化测试方法中的mockito/hamcrest参数匹配器?_Java_Spring_Unit Testing_Mockito_Hamcrest - Fatal编程技术网

Java 如何简化测试方法中的mockito/hamcrest参数匹配器?

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",

下面的测试方法出现在a中。 是否有一个不那么复杂的语法来编写这个测试,或者我如何将它分解成更小的块

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.