Do you like horror movies?
I do. Especially those Japanese horror movies. You know, when you have that creepy ghost kid, making your life miserable. Pops up behind, moves things around unexpectedly. And on occasion, kill the protagonist.
Mocks can be like that. Well, they probably won’t kill you, but they do have some kind of effects on their surrounding. And in Spring – it might look like paranormal activity.
See, much like our movie kid, if you supply a mock as a bean in Spring, it doesn’t die between tests. Oh, no. It lives on. Mainly to torment you.
You see you probably won’t notice it at first.
But if you go looking for things that should be in a certain way, they might have moved. A call counter may be higher than expected, or a behavior setting is lingering. Yeah, just like that kid.
There a couple of ways to handle that. Like resetting the mocks. Or using Spring Boot’s @MockBean. But like in those movies, you need to understand why those damn things keep coming back. Let’s just say that singletons are evil in this case. And there’s nothing good about the undead either. Like in most sequels.
So, if you want to be a mock star, and get rid of these annoying remnants, you really should check my course “API Testing with Spring“, I go into details about all these, and also a couple of horror flicks.
Knowledge is power, you know.