无法在Mockito中模拟任何东西

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

Can't Mock Anything in Mockito

问题

我是新手单元测试,尝试使用Mockito模拟一个映射。当我尝试模拟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 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

尝试使用列表和集合也会收到相同的错误。我尝试使用JDK 19也会得到相同的错误。

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.
英文:

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-2.html
匿名

发表评论

匿名网友

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

确定