简体   繁体   English

如何使用 Mockito 模拟记录

[英]How to mock a record with Mockito

I'm trying to mock a record class我正在尝试模拟记录类

    @Test
    public void testRecord() {
        record Rec(){}
        Mockito.mock(Rec.class);
    }

But it gives the error但它给出了错误

    org.mockito.exceptions.base.MockitoException:
    Cannot mock/spy class Rec
    Mockito cannot mock/spy because :
     - final class
        at ...

Which makes sense of course.这当然有道理。

As the error message suggests, you cannot mock final classes with Mockito.正如错误消息所暗示的那样,您不能使用 Mockito 模拟最终类。

This package brings this feature to Mockito :这个包为 Mockito 带来了这个功能:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <scope>test</scope>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM