Readit News logoReadit News
throwaway7375 commented on Stepping down as Mockito maintainer after ten years   github.com/mockito/mockit... · Posted by u/saikatsg
derriz · 3 months ago
I used ResultSet because the comment above mentioned it. A clearer example of what I’m talking about might be say you replace “x.size() > 0” with “!x.isEmpty()” when x is a mocked instance of class X.

If tests (authored by someone else) break, I now have to figure out whether the breakage is due to the fact that not enough behavior was mocked or whether I have inadvertently broken something. Maybe it’s actually important that code avoid using “isEmpty”? Or do I just mock the isEmpty call and hope for the best? What if the existing mocked behavior for size() is non-trivial?

Typically you’re not dealing with something as obvious.

throwaway7375 · 3 months ago
What is the alternative? If you write a complete implementation of an interface for test purposes, can you actually be certain that your version of x.isEmpty() behaves as the actual method? If it has not been used before, can you trust that a green test is valid without manually checking it?

When I use mocking, I try to always use real objects as return values. So if I mock a repository method, like userRepository.search(...) I would return an actual list and not a mocked object. This has worked well for me. If I actually need to test the db query itself, I use a real db

throwaway7375 commented on Stepping down as Mockito maintainer after ten years   github.com/mockito/mockit... · Posted by u/saikatsg
wiseowise · 3 months ago
> But third-party IO is exactly the thing you'd want to mock.

You write an adapter.

throwaway7375 · 3 months ago
Once you start writing adapters you need a way to instantiate them to choose between implementations, and factories is often used for this. Then you might generalize the test suites to make the setup easier and you end up with the infamous FactoryFactory pattern.
throwaway7375 commented on Stepping down as Mockito maintainer after ten years   github.com/mockito/mockit... · Posted by u/saikatsg
didip · 3 months ago
As someone who is not in the Java world, why does Java need a mocking library? Interface based polymorphism is not enough?
throwaway7375 · 3 months ago
Before Mockito, it was common (where I worked) to create an interface just to support testing. This is an anti-pattern in my opinion. To create interfaces just for testing complicates the code and it is one of my pet peeves. It also encourages the factory pattern.

I prefer Mockito's approach.

u/throwaway7375

KarmaCake day9December 28, 2025View Original