ArgumentCaptor并与hasItems进行比较

时间:2018-05-21 20:22:43

标签: java mockito hamcrest

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

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

enter image description here

1 个答案:

答案 0 :(得分:1)

p,你的机器上似乎出现了一些错误。根据您的测试,我创建了以下自包含的测试类:

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核:1.3
  • org.mockito:的Mockito核:2.18.3

我也留下了导入,因此您可以将它们与您的进行比较(以防万一“错误”导入导致错误)。当你从Hamcrest软件包中静态导入Matchers.*时,我很确定这个和/或你使用的库版本会导致你的问题。