Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)
I read a few threads here about static methods, and I think I understand the problems misuse/excessive use of static methods can cause. But I didn't really get to the bottom of why it is hard to mock static methods.

I know other mocking frameworks, like PowerMock, can do that but why can't Mockito?

I read this article, but the author seems to be religiously against the word static, maybe it's my poor understanding.

An easy explanation/link would be great.

1 Answer

0 votes
by (46k points)

I think the cause may be that mock object libraries typically generate mocks by dynamically creating classes at runtime (working cglib). This suggests they either implement an interface at runtime (that's what EasyMock does if I'm not confused), or they inherit from the class to mock (that's what Mockito does if I'm not confused). Both approaches do not work for inactive members since you can't reverse them using inheritance.

The only method to mock statics is to modify a class' byte system at runtime, which I guess is a little more involved than the estate.

That's my opinion at it, for what it's deserving...

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 4, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...