简体   繁体   中英

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.

This package brings this feature to Mockito :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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