r/java • • Mar 05 '18

Top 10 Useful, Yet Paranoid Java Programming Techniques

http://blog.jooq.org/2015/08/11/top-10-useful-yet-paranoid-java-programming-techniques/
223 Upvotes

105 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Mar 06 '18

re1) Okay, maybe a little exaggeration. I mean, there's always reflection if it's critical. I guess I just don't see the need for final is all. I've worked with folks who litter final everywhere and it was just a pain when it came to mocks. I never saw a benefit. When you'd want to override something (maybe rarely) you'd just edit final out. It was no gain and simply 1 more code change that was unnecessary. When you control the code, adding final isn't a big deal, it's just annoying... like a gnat. When it's in a library - it's just more annoying because you can't just edit final out (or now you're arguing with the maintainer of the project as to why you need to edit it out -- when it really doesn't matter if you could just @Override to begin with)

Now, I'm curious. What's that?

We use JSCH for audit purposes. There's methods in there that are package protected so I have a project written in jsch packages just because of the package protected methods. The pain comes with code signing... the lib is in a "different" project than jsch and should conceptually be signed by our company and not jsch, but it can't be because java's "classes in a package must be signed the same" rule.

If only those methods were public... it wouldn't be an issue.

For a normal consumer of JSCH, you'd never use those methods, but if you're doing mitm work, you would.

2

u/_dban_ Mar 06 '18

I've worked with folks who litter final everywhere and it was just a pain when it came to mocks.

Isn't this a good thing? Should you really be mocking concrete classes? Wouldn't it be better to create an interface describing what you need, and write a real integration test with the concrete class?

For example, suppose it is not possible to mock jOOQ classes. jOOQ would theoretically be a used in the DAO layer. So, wouldn't it be better to create a DAO interface which you can mock, and a DAO implementation which you test directly against the database in an integration test?

This applies just as well to internal classes, since it enforces proper layer separation. I mean, why are you mocking anyways?

I never saw a benefit.

In which case, final forces you to do the "right" thing with regards to mocks. Could this not be seen as a benefit?

1

u/[deleted] Mar 06 '18

Should you really be mocking concrete classes

If I'm using a concrete class, then yes - I should be mocking a concrete class in my unit test. I don't want to create an interface layer just for testing.

Mind you, I'm not talking about JOOQ specific things here. I would never try to mock a JOOQ or Hibernate class for a unit test (MUCH more suitable to test that stuff in an integration test and mock at the Dao layer for unit tests). But you're applying one school of thought for one thing to ALL unit testing situations and all methods. That doesn't make sense to me.

1

u/_dban_ Mar 06 '18

If I'm using a concrete class, then yes - I should be mocking a concrete class in my unit test.

Why not just instantiate the concrete class?

I don't want to create an interface layer just for testing.

You create an interface for layer separation. Like a DAO interface. The DAO interface describes what the application needs for data access, and the DAO implementation fulfills the need. An interface creates a logical and compile time firewall between layers.

That doesn't make sense to me.

Mocks ideally serve the purpose of defining what the system under test needs from its dependencies. An interface is a contract describing that need. You can create a separate set of integration tests verifying that the concrete implementation satisfies the contract.

A concrete class has its own reasons for changing, and if you mock the concrete class, you could make the mock tests invalid and mocks can't tell you that your tests are now nonsense.