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)