Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we mock a final class?

I am new to Java and haven't done much of unit testing in general.
Can someone tell me why final classes cannot be mocked?

like image 373
Abhi Avatar asked Mar 12 '23 09:03

Abhi


1 Answers

Most common mocking framework in the java world cannot mock final classes/methods because they are typically based on creating proxies. Creating proxies for final classes is not possible as we cannot subclass (extends) a final class.

However, there are some workarounds and solutions, many of which can be found here.

like image 156
Idos Avatar answered Mar 23 '23 09:03

Idos