简体   繁体   中英

How to mock classes annotated with @ConstructorBinding annotation

I have a configuration property class, annotated with @ConstructorBinding to make it immutable. But while running test cases, this is not getting mocked.

@ConfigurationProperties
@ConstructorBinding
@lombok.Value
class PropertyConfig{
// some code
}

While running the test classes, I'm getting the following error:

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

How to fix this error?

Your annotation @lombok.Value implies that your class is final and this is not directly supported by mockito.

Since this is a configuration class, it probably doesn't need to be mocked anyway, you can simply instantiate it by calling the constructor normally.

If you really need to mock it for some reason, see this question How to mock a final class with mockito , or do not use @Value .

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