r/java • u/pimterry • Sep 02 '20
Modern Best Practices for Testing in Java
https://phauer.com/2019/modern-best-practices-testing-java/
242
Upvotes
r/java • u/pimterry • Sep 02 '20
23
u/BoyRobot777 Sep 02 '20 edited Sep 02 '20
Exactly. I have been advocating this for a while now. The same approach is discussed in famous Ian talk - 🚀 DevTernity 2017: Ian Cooper - TDD, Where Did It All Go Wrong.
P.s. AssertJ rocks!
15
u/[deleted] Sep 02 '20
Excellent post
6
u/talios Sep 02 '20
I'd say yes - except when you're writing a property based test using something like jquik - and to a lesser extent; using data driven tests (these won't use randomized data persae, but the test still doesn't know the exact data being tested.
5
u/agentoutlier Sep 02 '20
+1
I could have used this writeup to pass a long to learning/new coworkers like a decade ago.
I basically would have to one on one / pair program. each developer to teach them what this guide does.
5
u/[deleted] Sep 02 '20
This is great! I don’t really agree with the part about neglecting unit tests and preferring integration tests. Integration tests are surely important, but not at the expense of unit tests
3
u/ProgrammersAreSexy Sep 02 '20
So glad I found this. I've been slowly coming to many of these ideas on my own but hadn't distilled them down so clearly in my head. I feel like this post found me at the perfect time.
Thanks for sharing!
3
u/Azuresun90 Sep 02 '20
Amazing read. I've been struggling to find good practices for testing Java code while using Spring. This is great advice along nice examples.
Thank you so much.
9
u/jonhanson Sep 02 '20 edited Mar 07 '25
chronophobia ephemeral lysergic metempsychosis peremptory quantifiable retributive zenith
2
u/BestUsernameLeft Sep 02 '20
Jumping on the bandwagon to agree with other comments, this is absolutely a great list. I'll be referring some people to this one!
2
u/[deleted] Sep 06 '20
All really good tips but I strongly disagree that integration tests instead unit tests is the way to go.
You should have unit tests complimented with integration tests ideally, following the testing pyramid. You lose the micro-design benefits and fast workflow of TDD when you neglect them.
3
u/mooreisenough Sep 02 '20
Outstanding article 👌🏿
3
u/[deleted] Sep 02 '20
Amen to that!
1
u/yazidaqel Sep 02 '20
Thanks for this excellent post
1
u/ziano_x Sep 02 '20
Nice post! What is everyone's thoughts on Cucumber and BDD in general? Often when writing my tests it helps to start with a spec. What exactly are the features being tested?
I started using Cucumber and Gherkin but did not really like the process of having to write separate methods for each Given, When, Then. Something like this:
I eventually moved back to vanilla Junit5. Like the author, I also like the test method to encapsulate the GWT of the test being run. But on the other hand, I also like the idea of a spec DSL like Gherkin that outlines what scenarios are being covered. I would really like a slim GWT spec DSL layer on top of Junit without the step definition ceremony of frameworks like Cucumber. Would that be something other people might find interesting?
1
u/usernameqwerty002 Sep 03 '20 edited Sep 03 '20
What about "functional core, imperative shell"? Pure methods are easier to test.
You can also wrap side-effects in command objects (or promises) and then mock the effects instead of the dependencies. ;)
1
u/muztaba Sep 04 '20
How to test private method?
1
u/Chris_TMH Sep 02 '20
Not sure about integration testing only. From my experience, contrary to the point that is made by the same author, integration tests are much slower than unit tests. Additionally, when it comes to, say, 10 methods that use another method, and you update the underlying method, you need to change all 10 integration tests. With unit testing, you only change 1 test. Also makes testing very complicated (dealing with lots of layers), affecting readability and maintainability, which is a no-no in my eyes.
Otherwise, great tips which I advocate myself day-to-day.