当我尝试某些 mockito 教程中的代码时,我遇到了问题。在下面,请查看必要的代码和错误,包括 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.test</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.25.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-maven-plugin -->
<!--<dependency>-->
<!--<groupId>org.liquibase</groupId>-->
<!--<artifactId>liquibase-maven-plugin</artifactId>-->
<!--<version>3.6.2</version>-->
<!--</dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.junit.Test;
import org.mockito.Mock;
import java.util.Iterator;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ParserTest {
@Test
public void update() throws IOException {
Iterator i = mock(Iterator.class);
when(i.next()).thenReturn("Hello").thenReturn("World");
String result = i.next()+" "+i.next();
assertEquals("Hello World", result);
}
}
堆栈跟踪如下:
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: interface java.util.Iterator.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.162-b12
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0_162-b12
JVM info : mixed mode
OS name : Windows 10
OS version : 10.0
Underlying exception : java.lang.IllegalArgumentException: Could not create type
任何想法都是这种行为的原因吗?
我该如何解决这个问题?
答案 0 :(得分:0)
如果您遵守Mockito's basic mocking principles,则很容易解决您的问题:
请确保还阅读详细说明,为什么不应该mock type[s] you don't own。
Teaser:特别是对于JVM中的集合或与集合相关的类,它不仅像模拟next()
那样模拟一种方法,还必须对hasNext()
进行一致的模拟防止不良的觉醒。 :-)
因此您的测试会缩小为类似的内容:
@Test
public void update() {
Iterator i = List.of("Hello", "World").iterator();
String result = i.next() + " " + i.next();
assertEquals("Hello World", result);
}
如果您在detailed docs中进一步阅读“ 1。让我们验证一些行为!” 说:
以下示例模拟了一个List,因为大多数人都熟悉该界面(例如add(),get(),clear()方法)。 实际上,请不要嘲笑List类。改用真实实例。“
答案 1 :(得分:0)
您可能会在堆栈跟踪下方看到另一个“由...引起的”错误,以提供真正的原因。