0 votes
1 view
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 (46.1k 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
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...