ArgumentCaptor 并与 hasItems 进行比较

ArgumentCaptor and comparing with hasItems

我已经绝望了,我找不到为什么这个测试没有被评估为成功。我已经检查了百万次:

package someptest;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import java.sql.SQLException;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import somep.Order;
import somepBO.BOException;
import somepdao.OrderDAO;

public class XXX {

@Mock
OrderDAO dao;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
}

@Test
public void captor_A() throws SQLException, BOException {
    Order order = new Order();
    ArgumentCaptor<Integer> intCaptor = ArgumentCaptor.forClass(Integer.class);
    when(dao.read(any(Integer.class))).thenReturn(order);
    dao.read(123);
    dao.read(456);
    verify(dao, times(2)).read(intCaptor.capture());
    @SuppressWarnings("unused")
    List<Integer> xs = intCaptor.getAllValues();
    assertThat(intCaptor.getAllValues(), hasItems(456));
}
}

这是我的调试屏幕,captor 捕获了正确的值,但 assertThat 不接受它,为什么?

哎呀,你的机器似乎出了点问题。根据您的测试,我创建了以下独立测试 class:

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.List;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

public class ArgumentCaptorTest {

    private static class Order {
    }

    public static class OrderDAO {
        public Order read(Integer any) {
            return null;
        }
    }

    @Mock
    OrderDAO dao;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void captor_A() {
        Order order = new Order();
        ArgumentCaptor<Integer> intCaptor = ArgumentCaptor.forClass(Integer.class);
        when(dao.read(any(Integer.class))).thenReturn(order);
        dao.read(123);
        dao.read(456);
        verify(dao, times(2)).read(intCaptor.capture());
        assertThat(intCaptor.getAllValues(), hasItems(456));
    }
}

使用的依赖项(pom.xml):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>testing</groupId>
    <artifactId>testing</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.18.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

当我开始测试时,它运行完美,并给我一个绿色的条。就我所见,你做对了。也许您在 JUnit、Hamcrest 和 Mockito 之间存在导致错误的版本冲突?我使用了这些 JAR 版本:

  • junit:junit:4.12
  • org.hamcrest:hamcrest-core:1.3
  • org.mockito:mockito-core:2.18.3

我还留下了导入,以便您可以将它们与您的进行比较(以防 "wrong" 导入导致错误)。当您从 Hamcrest 包静态导入 Matchers.* 时,我很确定这个 and/or 您使用的库版本会导致您的问题。