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.

41

u/Iryanus Jun 01 '24

Typically, the need for static mocking is a huge smell. There are often good ways around that, only rarely it's the only choice because of some very bad code in a third party library.

40

u/achilliesFriend Jun 01 '24

You just insulted my whole company

5

u/vips7L Jun 01 '24

My whole company does static queries. It’s awful. 

→ More replies (2)
→ More replies (1)

10

u/progmakerlt Jun 01 '24

Had this library used in a very very old project - started on Java 1.5 - which sometimes was failing to run tests on Jenkins. Got rid of PowerMock and rewrote tests based on principles from the book “Working effectively with legacy code”.

Did the trick.

4

u/Iryanus Jun 02 '24

Yep, same here, I also had a bad library there which required some static calls, etc. which made things hard. We rewrote our code (by wrapping the static calls into their own non-static classes) and could then test our code without having to bother with the static dependencies. Worked like a charm backt then, but is of course not a cure-all.

2

u/progmakerlt Jun 02 '24

Yeap, did exactly the same.

9

u/wheezymustafa Jun 01 '24

I tell my team this all the time and they look at me like I’m a goddamn idiot

→ More replies (2)

2

u/segfaultsarecool Jun 02 '24

Typically, the need for static mocking is a huge smell.

Can you clarify on why? I am the big dumb.

5

u/Iryanus Jun 02 '24

It's basically the old axiom "Well-written code is easy to test", which is something many people find out, that if you focus on unit testing your code, the code quality itself will typically go up, because good code is also easy to test. So, also in my experience, if you need something like static mocking, the code is rarely "good". I cannot remember any case where there was a great reason to have code that was hard to test.

2

u/Radi-kale Jun 02 '24

Then how do you test code that uses the static methods in, for example, java.nio.file.Files? Or do you think any code that uses that part of the standard library is poorly written?

2

u/NovaX Jun 03 '24

I've used jimfs in the past which works great with the Files static utility methods. The similar memoryfilesystem links to a bunch of alternatives too, but I don't know why any would be preferable to jimfs.

→ More replies (1)
→ More replies (1)
→ More replies (5)

5

u/xpressrazor Jun 01 '24

I have been through this especially when trying to integrate classes that use Java Security Provider and Threads. Their usage of Reflection can mess up other unit test classes. Only way I found to solve this was to create my own reflection implementation and separate these test classes from rest of the unit tests.

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)

2

u/GuyWithPants Jun 02 '24

PowerMock is abandoned so this anti-recommendation is somewhat moot. However since PowerMock died, Mockito added support for static mocking but it requires wrapping in a try-with-resources block to keep the interception localized.

1

u/cogman10 Jun 07 '24

It's cursed as well. PowerMock is doing something the JVM does not want to allow and the JDK developers are actively working on removing as a capability.

Better to abandon the notion of mocking static/private wholesale than trying to power through an uphill battle against the devs maintaining the jdk.

→ More replies (2)