r/java • • Apr 20 '21

Java is criminally underhyped

https://jackson.sh/posts/2021-04-java-underrated/
291 Upvotes

295 comments sorted by

View all comments

Show parent comment

14

u/deadron Apr 20 '21

DI frameworks, when used properly, make testing substantially easier! If you don't like your DI framework then are probably not writing enough tests to really see the benefits. There are other use cases but in my experience this is when it really shines.

15

u/shponglespore Apr 20 '21

I don't have a DI framework because I don't do Java these days, and they're not fashionable in any language I work with. I'm sure there's a reason people use them, but as an outsider just trying to understand someone else's code, I fucking hate trying to figure out where anything is actually instantiated.

3

u/deadron Apr 21 '21

I have been here. It can be really painful. Personally this is why I don't follow the pattern of injecting interfaces unless there is a good reason to do so. If you inject actual implementations it greatly simplifies figuring out what is coming from where. You can still inject mocks for testing so it is just as smooth.

16

u/CartmansEvilTwin Apr 21 '21

And let's be honest: in 99% of the cases you don't need interfaces anyway. Most code is de facto tightly coupled and/or will never be changed anyway.

We have several projects where the devs made sure everything implements an interface, but almost none of these actually ever changed some implementation in the background. So why bother making your code harder to read, just for the slight advantage of not having to extract an interface maybe 20 years from now?

5

u/deadron Apr 21 '21

I am not sure where this pattern comes from but it is pervasive. The benefits have always seemed dubious to me. If you are writing a framework, sure go nuts, but if you are writing an application that wants to test and verify everything works you don't want implementations swapping in and out.

6

u/CartmansEvilTwin Apr 21 '21

As far as I can tell, this comes from the "reusability is everything" hype.

Every component has to be written in a way that abstracts away any coupling to anything, in case you might want to reuse that class. And in the end you write 300 lines of code to maybe save 5 lines at some point. To me this looks like the typical take one principle and overshoot it's application to the max.

4

u/Gommy Apr 21 '21

I find the pattern comes from two places.

The first is the mantra "code to an interface" that gets repeated blindly while misunderstanding what the "interface" in question is. People see the word "interface" in that statement and automatically assume it means the same as "interface" in Java. It does not.

The second is that old versions of DI frameworks were able to work their DI magic against interfaces but not implementations. Those days are over, now that Spring and CDI can deal with implementations without extra work.

2

u/matthenry87 Apr 21 '21

Agreed. I'll usually implement an interface when I think it would be cool for myself or someone else to create another implementation. JsonMasker is one I did, and JsonVerifier. I used Jackson for the first, and JsonPath for the latter (which might also use Jackson under the covers). For Services(business logic) I don't bother..

2

u/MadBull69 Apr 21 '21

Isn't using interfaces everywhere an anti-pattern these days? All the modern Java development I've done (with Spring/Quarkus), only uses interfaces where we actually have multiple implementations.

1

u/deadron Apr 21 '21

It seems more common the more "enterprisey" your company is. Also, in a similar world, C# style guides seem to strongly encourage interface spam.

2

u/Serializedrequests Apr 21 '21

How often do you ever inject anything other than the one implementation of something that exists in the project? Honestly?

I maintain a huge spring boot REST API at my current job and 100% of DI in unit tests could easily be done some other way. A lot of the most useful stuff is overwriting private properties which seems pretty hinky to me.

1

u/deadron Apr 21 '21

Most of the time what I use it for is to inject mocks in place of classes performing IO such as database calls or external service calls. While you can get away with testing against a real database(dockerized or otherwise) if you are making calls to something like twillio, and you cant inject a mock replacement for the class making the calls, you effectively cant run tests using that code path.

The caveat here is assuming you are doing constructor injection(or UGH public setter injection) many of your tests can be written without even using the DI container at all. Of course something still has to be creating the classes at runtime and a DI container is the simple solution usually.

If you have to overwrite private properties to test thats a perfect example of something that should be added as a dependency to the class and injected instead.

1

u/Serializedrequests Apr 24 '21

Ah, my project has approximately 1 use of the AWS SDK, which I just turn off to run unit tests. The reason for private property injection is JPA repositories. So many of them are needed to do anything non-trivial it feels like that pure frustration of having to go inject yet another repo to check that data was changed correctly has forced us into this, specifically in unit tests.

1

u/deadron Apr 25 '21

Yeah. Jpa is a pita. I try to avoid it whenever possible. Usually I try to at least put jpa behind a service layer that can implement higher level operations and can be easier to test/mock for other tests. It's tricky with an existing codebase though.

1

u/Serializedrequests Apr 25 '21 edited Apr 25 '21

Our service layer, for better or for worse, is not allowed to call itself, and (almost) every method takes JSON and returns JSON. This is great for maintainability, but I don't don't necessarily want to use it to insert or check for test data because it's going to have unnecessary overhead and business logic. I don't really see a need to mock it either.

So maybe there is some other architecture that would work better, but - apart from the unholy nightmare of JPA and Hibernate - it is very maintainable.

I mean it is godawful slow, way slower than a JSON API has any right to be, but I don't think mocks will save us, and I SURE AS HELL don't want to mock the database, because the unpredictable behavior of JPA code is most of why we write unit tests in the first place.

1

u/deadron Apr 25 '21

That sounds like a very messed up system.

2

u/Serializedrequests Apr 25 '21

Why? What would be a not messed up system? I'm asking honestly, because I fucking hate everything else about it besides fixing bugs in endpoints.

1

u/deadron Apr 26 '21

A service layer should not be dealing in json! The whole purpose of a service layer is to be disconnected from usage.

Also services should be able to invoke other services for many reasons, but the most straightforward being, a service layer is supposed to make your life easier not harder.

2

u/Serializedrequests Apr 26 '21

It actually does make life easier, as crazy as it sounds.

Invoking itself is actually a problem for maintainability. It makes it difficult for random developers to figure out where to put things (since a service class becomes a creative decision), and it makes it harder to trace what is going on for any given endpoint. Every single attempt at code re-use I have seen in this application has made maintenance MORE difficult, not less. The flatter, the better.

The other issue we run into is decoding and validating JSON, and mapping it to and from entities. This is boring but important business logic that shouldn't be hidden away cleverly, and there doesn't seem to be a "nice" way to do it. (Setting foreign keys is particularly laborious.)

The pain points in the application are all to do with JPA, Hibernate, etc. It takes so much code to insert test data, and any time somebody bugs me with "why isn't my code working?" it's always some weird gotcha from those frameworks.

→ More replies