gpt4 book ai didi

java - Mockito.eq 匹配器似乎匹配列表引用,而不是值

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:09:35 25 4
gpt4 key购买 nike

我有一个方法 addToClonedList(List)在类里面App调用方法 add(List)在类里面Dependency .我要单元测试 addToClonedList(List) ,我想创建一个模拟 Dependency对象(称为 mockDependency )并验证方法 mockDependency.add(List)使用正确的参数调用。然而,由于 List clonedList 引用的实例在有问题的调用之后获取一个新元素( 100 )添加到 if 中,Mockito 认为 2 元素 List被传递给了mockDependency , 测试失败。实际上,List传递给 mockDependency.add(List)调用时只包含一个元素。这是预期的 Mockito 行为吗?有没有更标准的方法来测试我是否通过了正确的 ListmockDependency.add(List)

在下面查看我的代码结构和测试输出:

ma​​in/java/App.java:

import java.util.*;

public class App {
Dependency dataLayer = new Dependency();

void addToClonedList(List<Integer> integerList) {
List<Integer> clonedList = new ArrayList<Integer>(integerList);
dataLayer.add(clonedList);
clonedList.add(100);
}
}

ma​​in/java/Dependency.java:

import java.util.List;

public class Dependency {
void add(List<Integer> integerList) {
//
}
}

测试/java/AppTest.java:

import java.util.*;
import org.junit.*;
import org.mockito.*;

public class AppTest {

@InjectMocks
App app;

@Mock
Dependency mockDependency;

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

@Test
public void testWithVerify() {
List<Integer> originalList = new ArrayList<>();
originalList.add(1);

app.addToClonedList(originalList);

Mockito.verify(mockDependency).add(Mockito.eq(originalList));
}
}

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>com.athenahealth</groupId>
<artifactId>mockitotest</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.testifyproject.mock</groupId>
<artifactId>mockito</artifactId>
<version>0.9.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

</project>

测试输出:

Argument(s) are different! Wanted:
mockDependency.add([1]);
-> at AppTest.testWithVerify(AppTest.java:25)
Actual invocation has different arguments:
mockDependency.add([1, 100]);
-> at App.addToClonedList(App.java:8)

Comparison Failure:
Expected :mockDependency.add([1]);
Actual :mockDependency.add([1, 100]);

最佳答案

As Andy Turner notes ,问题是 Mockito 只保留对传递值的引用,而不克隆它。这是有道理的,因为它对于像 String 这样的不可变值对象来说是一种浪费,而且对于像 Thread 这样的某些引用实际上是不可能的。

但是,您可以编写自己的答案来自己制作该副本:

List<Integer> listSnapshot;

@Test
public void testWithVerify() {
List<Integer> originalList = new ArrayList<>();
originalList.add(1);
Mockito.when(mockDependency.add(any())).thenAnswer(invocation => {
listSnapshot = new ArrayList<>((List<Integer>) (invocation.getArguments()[0]));
});

app.addToClonedList(originalList);

assertEquals(originalList, listSnapshot);
}

关于java - Mockito.eq 匹配器似乎匹配列表引用,而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47705605/

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