What are the efficient ways to improve and learn more about Java unit testing? Is there a repository where I can practice Unit Testing with ways how to solve it, so I can compare it with mine? How did you practice doing it? As sometimes when I'm doing stuff I get stuck and not able to test the other parts.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (6)
There are some principles when writing tests in general. They're so very basic it seems almost stupid, but I find that I constantly have to remind myself of them in order not to get dragged away:
Testing really is something that takes practice. The more tests you write, the better you'll become. Just make sure you get the basics straight. Frameworks (JUnit is totally fine, but get a good assertion library) and best practices can get you started, but you'll need to work for that "gut feeling" what to test on your own. It will come in time.
Agreed on all the principles, and I want to take action to improve my testing skills.
Do you have a recommendation for my next steps? A practical step to efficiently practice? What I currently do is look for open source projects and read on how they test but it feels like it's a passive thing to do, so I want to explore different paths I can take to actively practice and if I failed to create a test for something, I can look and see some solution.
I can recommend to try some code katas or very simple things like write a fraction class and try to use tests to implement it. It's very helpful and see how testing and implementing works in combination. Yeah it sounds useless to write such a class...but it helps practicing writing tests etc. because the problem domain is (more or less) simple....
Another level of going is to follow the principles of Kent Beck (read the book and try to write the previously mentioned Fraction class by using the TDD principles and of course practice, practice, practice....
Awesome! Will try out those code katas 💪
It’s important to consider the difference between unit tests and other forms of testing. You should be testing individual modules in isolation. If you’re not, it’s not a unit test. Tools like Mockito can help a lot in Java.
To get great at unit tests means you understand the scope of what you’re testing. Don’t just test the happy path. Try to find as many edge cases as you can. It is much easier to fix an issue if it arises during a unit test vs any other test or, even worse, a customer box.
Try not to put too much in a single unit test. A good unit test should be self documenting. Try to avoid excessive commenting. Unit tests are not the place to get clever. This is usually a sign that you may need to split up your test cases.
I also have a long way to go in testing but, in unit tests, I try to make testXXX to every public method, with parameters of the happy path and with invalid input. Obviously the wrong input scenarios should expand over time and the methods should still work as expected.