英文:
Error when trying to use @Spy Annotation: java.lang.NullPointerException: Cannot read the array length because "elementData" is null
问题
I am using the @Spy annotation and the following error appear when i'm trying to run the test:
我正在使用@Spy注解,但当我尝试运行测试时出现以下错误:
java.lang.NullPointerException: Cannot read the array length because "elementData" is null
java.lang.NullPointerException: 无法读取数组长度,因为"elementData"为空
This is the code i have:
这是我的代码:
package com.tddk.junit.service;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Spy;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.ArrayList;
import java.util List;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@Slf4j
@ExtendWith(MockitoExtension.class)
public class StudentServiceTest {
@Spy
List<String> spyList = new ArrayList<String>();
@Test
void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
spyList.add("one");
spyList.add("two");
verify(spyList).add("one");
verify(spyList).add("two");
assertThat(spyList).hasSize(2);
}
}
The sample code above is from: https://www.baeldung.com/mockito-spy I do exactly as the tutorial said but still getting an error. Can someone show me how to fix it please.
上面的示例代码来自:https://www.baeldung.com/mockito-spy 我按照教程中的指示进行了操作,但仍然遇到错误。能有人向我展示如何修复它吗?
英文:
I am using the @Spy annotation and the following error appear when i'm trying to run the test:
java.lang.NullPointerException: Cannot read the array length because "elementData" is null
This is the code i have:
package com.tddk.junit.service;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Spy;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@Slf4j
@ExtendWith(MockitoExtension.class)
public class StudentServiceTest {
@Spy
List<String> spyList = new ArrayList<String>();
@Test
void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
spyList.add("one");
spyList.add("two");
verify(spyList).add("one");
verify(spyList).add("two");
assertThat(spyList).hasSize(2);
}
}
The sample code above is from: https://www.baeldung.com/mockito-spy I do exactly as the tutorial said but still getting an error. Can someone show me how to fix it please.
答案1
得分: 1
这可能是因为您正在使用JDK 17或更高版本,而文章的作者正在使用较低版本的JDK。
在JDK 17之后,越来越多的用例出现了问题,Spy就是其中之一。您可以切换到内联的mockmaker以避免这个问题。
切换到内联mockmaker
- 在
test
目录内创建resources
目录。 - 然后在
resources
目录内创建一个名为mockito-extensions
的目录。 - 在
mockito-extensions
目录内创建一个名为org.mockito.plugins.MockMaker
的文件。 - 最后,在
org.mockito.plugins.MockMaker
文件中添加文本mock-maker-inline
。
文件夹结构应该如下所示:
此外,我的建议是不要对列表进行监视,而是在实际列表上执行检查。
英文:
This could be because you're using JDK 17 or plus, and the author of the article is using a lower JDK version.
After the JDK 17, more and more use cases are broken, and Spy is one of them. You can switch to the inline mockmaker to avoid this issue.
Switching to the inline mockmaker
- Create the
resources
directory inside thetest
directory. - Then Create a
mockito-extensions
directory withinresources
directory. - Create a file named
org.mockito.plugins.MockMaker
withinmockito-extensions
directory. - Finally, add this text
mock-maker-inline
to theorg.mockito.plugins.MockMaker
file.
Folder structure should look like this:
Refer here as well: Switch the default mockmaker to the inline mockmaker on JDK 17+
Lastly, my advice is to not spy on lists, but instead perform checks on the real list.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论