无法在Mockito中模拟任何东西。

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

Can't Mock Anything in Mockito

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对单元测试还不熟悉,尝试使用Mockito来模拟一个map。当我尝试模拟ConcurrentHashMap时,它会抛出如下错误。不仅对HashMap,对所有对象都会抛出错误。

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ConcurrentHashMap;

import static org.mockito.Mockito.mock;

class UtilsTest {


    @BeforeEach
    void setUp() {
        ConcurrentHashMap mockedMap = mock(ConcurrentHashMap.class);
    }

    @Test
    void lessLoadedServersCount(){

    }
}
org.mockito.exceptions.base.MockitoException: 
Mockito无法模拟这个类:class java.util.concurrent.ConcurrentHashMap。

Mockito只能模拟非私有且非final的类。
如果您不确定为什么会出现此错误,请报告给邮件列表。


Java               : 11
JVM厂商名称        : OpenLogic
JVM厂商版本        : 11.0.19+7-adhoc.admin.jdk11u
JVM名称           : OpenJDK 64-Bit Server VM
JVM版本           : 11.0.19+7-adhoc.admin.jdk11u
JVM信息           : 混合模式
操作系统名称      : Mac OS X
操作系统版本      : 13.2.1



底层异常 : java.lang.UnsupportedOperationException: 无法使用反射定义类:找不到sun.misc.Unsafe

尝试使用List和Set也会得到相同的错误。我尝试使用JDK 19也得到相同的错误。

org.mockito.exceptions.base.MockitoException: 
Mockito无法模拟这个类:class java.util.ArrayList。

Mockito只能模拟非私有且非final的类。
如果您不确定为什么会出现此错误,请报告给邮件列表。
org.mockito.exceptions.base.MockitoException: 
Mockito无法模拟这个类:class java.util.HashSet。

Mockito只能模拟非私有且非final的类。
如果您不确定为什么会出现此错误,请报告给邮件列表。
英文:

I'm new to Unit testing and Tried to Mock a map using Mockito.

When I tried to mock ConcurrentHashMap it throws error like this . Not only Hash map it throws error for all Object .

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ConcurrentHashMap;

import static org.mockito.Mockito.mock;

class UtilsTest {


    @BeforeEach
    void setUp() {
        ConcurrentHashMap mockedMap = mock(ConcurrentHashMap.class);
    }

    @Test
    void lessLoadedServersCount(){

    }
}
org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class java.util.concurrent.ConcurrentHashMap.

Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.


Java               : 11
JVM vendor name    : OpenLogic
JVM vendor version : 11.0.19+7-adhoc.admin.jdk11u
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 11.0.19+7-adhoc.admin.jdk11u
JVM info           : mixed mode
OS name            : Mac OS X
OS version         : 13.2.1



Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe

Tried With List and Sets as well getting same error . I tried with JDK 19 getting same error.

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class java.util.ArrayList.

Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class java.util.HashSet.

Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.

答案1

得分: 2

我们使用的是Mockito的哪个版本?

我在这个GitHub仓库中创建了一个示例,对ConcurrentHashMap进行模拟似乎运行良好。

class UtilTest {

    private ConcurrentHashMap map;

    @BeforeEach
    void setUp() {
        map = mock(ConcurrentHashMap.class);
    }

    @Test
    void test(){
        when(map.get("key")).thenReturn("value");

        assertEquals("value", map.get("key"));
    }
}
英文:

Which version of Mockito do you use?

I create an example in this GitHub repository and mocking ConcurrentHashMap seems to work fine.

class UtilTest {

    private ConcurrentHashMap map;

    @BeforeEach
    void setUp() {
        map = mock(ConcurrentHashMap.class);
    }

    @Test
    void test(){
        when(map.get("key")).thenReturn("value");

        assertEquals("value", map.get("key"));
    }
}

huangapple
  • 本文由 发表于 2023年8月9日 14:58:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865315.html
匿名

发表评论

匿名网友

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

确定