Thursday, July 20, 2017

Mockito and Powermockito integrated properly, soon!

Authors of libraries that are integrated should work together on the APIs. This way, the products are of higher quality and the integration is safe for their users. I published this article originally on LinkedIn.

Enable static mocking in Mockito? This dilemma might be moot very soon.

Powermockito is a part of Powermock, a library that helps mocking static methods and other unmockable things. After many years of developing Powermockito in isolation, we now work together on exposing new public APIs from Mockito, so that using Powermockito is safe. Why is it unsafe now? Powermockito uses Mockito internal APIs and copies some Mockito code. The resulting solution is brittle, very susceptible to version changes and falls over on various edge cases. Arthur Zagretdinov, current maintainer of Powermockito engaged with Mockito team and we are making nice progress on cleaning this up. For Mockito team, it is a challenging engineering exercise. We need to design the integration APIs so that 3rd party tools such as Powermock can implement clean solutions on top of Mockito.

Opinionated but not dogmatic

Disclaimer: Mockito team thinks that the road to hell is paved with static methods. However, Mockito's job is not to protect your code from static methods. If you don’t like your team doing static mocking, stop using Powermockito in your organization. Mockito needs to evolve as a toolkit with an opinionated vision on how Java tests should be written (e.g. don't mock statics!!!). However, Mockito is not dogmatic. We don't want to block unrecommended use cases like static mocking. It's just not our job.

Clean integration needs teamwork

Powermockito currently has code copied from Mockito. When we finish, there will be zero code copied from Mockito. That's how it should have been done long time ago. When a library needs to deeply integrate with other library, the teams should cooperate rather than copy the code over and use internals. I absolutely respect Powermockito developers - sometimes you have to use internal APIs to make progress. The time we can allocate to Open Source is sometimes a fraction of what is actually needed to develop a clean solution. Even if Powermockito pushed Mockito team harder on providing the necessary APIs, there is no guarantee Mockito core developers have had the time to implement them. Fortunately, in mid 2016 Mockito team got some fresh blood. We shipped Mockito v2 in October last year and we continue shipping nice incremental improvements ever since. For example, check out strict stubbing feature for cleaner tests and improved productivity. Now it's time to sort out the integration with Powermockito.

Arthur, keep the steam up and let's complete this project! I promise that Mockito team will find time to implement the APIs. Exciting!

No comments: