Error when trying to use @Spy Annotation: java.lang.NullPointerException: Cannot read the array length because "elementData" is null

huangapple go评论89阅读模式
英文:

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&lt;String&gt; spyList = new ArrayList&lt;String&gt;();

    @Test
    void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
        spyList.add(&quot;one&quot;);
        spyList.add(&quot;two&quot;);

        verify(spyList).add(&quot;one&quot;);
        verify(spyList).add(&quot;two&quot;);

        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

  1. test目录内创建resources目录。
  2. 然后在resources目录内创建一个名为mockito-extensions的目录。
  3. mockito-extensions目录内创建一个名为org.mockito.plugins.MockMaker的文件。
  4. 最后,在org.mockito.plugins.MockMaker文件中添加文本mock-maker-inline

文件夹结构应该如下所示:

Error when trying to use @Spy Annotation: java.lang.NullPointerException: Cannot read the array length because "elementData" is null

此外,我的建议是不要对列表进行监视,而是在实际列表上执行检查。

英文:

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

  1. Create the resources directory inside the test directory.
  2. Then Create a mockito-extensions directory within resources directory.
  3. Create a file named org.mockito.plugins.MockMaker within mockito-extensions directory.
  4. Finally, add this text mock-maker-inline to the org.mockito.plugins.MockMaker file.

Folder structure should look like this:

Error when trying to use @Spy Annotation: java.lang.NullPointerException: Cannot read the array length because "elementData" is null

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.

huangapple
  • 本文由 发表于 2023年3月9日 23:48:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定