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

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捕获了正确的值,但是assert那不接受它,为什么?

java - ArgumentCaptor并与hasItems比较-LMLPHP

最佳答案

ew,您的计算机似乎出了点问题。根据您的测试,我创建了以下独立的测试类:

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


我还离开了导入,以便您可以将它们与您的导入进行比较(以防万一导入“错误”会导致错误)。当您从Hamcrest包中静态导入Matchers.*时,我非常确定此版本和/或您使用的库版本会引起问题。

关于java - ArgumentCaptor并与hasItems比较,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50456099/

10-12 14:25