gpt4 book ai didi

java - ArgumentCaptor 并与 hasItems 进行比较

转载 作者:行者123 更新时间:2023-12-02 11:13:21 27 4
gpt4 key购买 nike

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

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 不接受它,为什么?

enter image description here

最佳答案

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com