r/java • • Jun 01 '24

What java technology (library, framework, feature) would not recommend and why?

165 Upvotes

464 comments sorted by

View all comments

124

u/progmakerlt Jun 01 '24 edited Jun 01 '24

PowerMock. Ability to mock static methods is cool, but can lead to disastrous results, when tests pass once and then randomly fail.

Edit: typo.

14

u/agentoutlier Jun 01 '24

I will just add that "mocking" especially using a library (and especially the one mentioned) should be the last resort after you have eliminated all other options.

I find custom built mocks like Spring's Servlet Mocks acceptable but still rather use the real thing.

13

u/DelayLucky Jun 01 '24

This.

We have an official guideline to:

  1. Use real if you can (save the "but it's not unit test" argument)

  2. Use fake if real is too hard to use

  3. Use mock as the last resort.

Although, despite that, mocks are still overused because it's usually the most accessible and most widely known by junior devs. There's also "old habit die hard".

Sometimes the real is indeed hard to use (lots of deps for example). And people generally are too lazy to build a fake (do I have to implement all these abstract methods?).

And mock (we use Mockito) is like "who doesn't know how to use one?".

I wish I could give it more publicity: in Mockito, using the misleadingly named @Spy (which immediately sets off alarm when anyone first hear it), I contributed the feature of using an abstract class to build a fake without having to implement all methods.

It's meant to lower the bar of entry for fakes significantly. Compared to manual doAnswer(), it's type safe and generally reads clearer.

8

u/Paulus_cz Jun 01 '24

God I hate mocking with passion. Don't get me wrong, when there is no other option, go for it, but I have seen WAY TOO MANY "unit tests" which mock EVERYTHING the method calls and check the mock was called in a specific way - such tests is testing if the method is WRITTEN the way it is written. Good luck refactoring anything.
I get it, it is simple, you can just write them without thinking about anything the method calls or is called by, you get 100% coverage and you do not have to engage your brain once. Hell, I am pretty sure I could write a script for writing these "tests", which is why they are TOTALLY USELESS CRAP.
Had to get this off my chest...

6

u/DelayLucky Jun 01 '24 edited Jun 01 '24

You are not alone.

In my workplace this is called "change detector test" and is advertised against officially.

Although, the force of least resistance path is too great that despite all these guidance, such pointless tests are still rampant (only gets worse with the recent company-wide cutting corners and divesting infra/core teams).

I find that junior devs more likely fall for this kind of testing, because using mocks is just so easy and they get to "check it off the list".

And that's another bad thing about these tests besides them being pointless and getting in the way of refactoring: they make essentially untested code look like tested. Most of the usual testing metrics become meaningless after diluted by them.

It also correlates with the unhealthy dogma against integration tests, as if they were somehow a bad thing. Junior devs who like to write change detector tests tend not to write integrationt tests, both habitually and also because it takes a lot more effort and skill to write integration tests.

I sometimes wonder how the whole leetcode-focused hiring practice got us here. The overall know-how of common best practices feels to be on a downtrend lately. With the company shifting focus from core/infra, perhaps it's time to also shift the hiring focus from finding those who can binge leetcode to those who can write decent-quality code. Stop pretending that we'll be able to teach best practices easily on the job.

2

u/NovaX Jun 01 '24

I believe much of that thinking was due to early mocking libraries like EasyMock which required expectations and verification of every minuscule detail. The library was sometimes convenient, but other times it was cleaner to write a mock by hand. Mockito inverted that and made mocking much less brittle, but there was a long transition period. That had cultural imprints which led to a dislike towards mocking, a lot of hype on "best practices" which became bad ones, many clones in other languages that also had to unwind from poor defaults, and a lot of legacy test code that was painful to work with. I find using mocking sparingly to be quite nice, but certainly not as much as in the past or with those absolutist approaches to testing.

3

u/DelayLucky Jun 01 '24

Oh EasyMock is a distant past for us. When we talk about "prefer real, then fakes and only mock as last resort", we do mean Mockito mocks.

It's less brittle yes, but as long as you write when(service.getFoo(any()).thenReturn(...), you are still implementing the dependency service based on your own assumption of it, which you also used in production code. If you assumed wrong, both the test and prod code are wrong.

In contrast, using the real service (there are techniques that can start up the service in a hermetic environment), if you send an invalid field, the service will tell you; if they make a breaking change, the test will fail.

You get similar benefit if you use a high-fidelity fake (ideally maintained by the same team that own the service).

→ More replies (4)
→ More replies (2)
→ More replies (2)

17

u/SignificantAd9059 Jun 01 '24

It really depends on what you are mocking. It’s perfectly acceptable to mock services that you are not directly testing. Otherwise your making an integration test which is also useful but definitely not the same thing

5

u/DelayLucky Jun 01 '24 edited Jun 01 '24

Only if these questions are out of scope for your test:

  • Am I sending the right request to the service for what I'm trying to do, with all required fields set correctly ? (e.g. charge a credit card).

  • Am I interpreting the response correctly? Is the value I expect coming in this particular field?

And if this unit test isn't concerned of these aspects, you better make sure there are tests that do verify them.

Too often I've seen people just assuming that they must be interacting with a service in the right way. And bugs often come out of those cracks of the system becasue what are the odds that two teams of different engineers happen to align without talking to each other once?

"Integration tests", "unit tests", they are just names. At the end of day you want your production to work as expected. Customers don't want to hear "I've coded my client code according to the document of this dependent service, it's their fault the system didn't work out".

→ More replies (6)

5

u/LC_From_TheHills Jun 01 '24

Yeah this is weird. Use dependency injection. Then mock those in the class you are testing. I’ll mock my own classes— I’ll write unit tests for those specifically.

You don’t need to test your dependencies. If the integration of your dependencies is so flaky then you have different problems.

Save the “real” stuff for integration tests in your pipeline at a pre-prod stage. When you’ve actually deployed your code somewhere.

→ More replies (12)
→ More replies (3)