问题描述
当我尝试一些模拟教程中的代码时,我遇到了问题。下面,请参阅必要的代码和错误,包括pom.xml和堆栈跟踪。
<小时 />我的绒球.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
我的问题:
知道这种行为的原因是什么吗?
如何解决此问题?
如果你遵守Mockito的基本嘲笑原则,你遇到的问题很容易解决:
- 不要嘲笑你不拥有的类型
- 不要模拟值对象
- 不要嘲笑一切
- 用你的测试表达爱!
请务必阅读详细说明,为什么您不应该模拟您不拥有的类型。
预告片:对于JVM中的集合或与集合相关的类,仅仅像next()
那样嘲笑一种方法是不够的,还必须对hasNext()
进行一致的嘲笑以防止不良觉醒。
因此,您的测试将缩小到如下所示:
@Test
public void update() {
Iterator i = List.of("Hello", "World").iterator();
String result = i.next() + " " + i.next();
assertEquals("Hello World", result);
}
如果您在详细文档中进一步阅读,请">1.让我们验证一些行为!说:
下面的例子模拟了一个List,因为大多数人都熟悉这个接口(比如add(),get(),clear()方法)。实际上,请不要嘲笑 List 类。 请改用真实实例。