java – How to master testing?

Question:

I'm relatively new in development, and a few weeks ago I thought about not testing my code. Disorder. Started reading Kent Beck's book about TDD. I caught the main idea – "red" – "green" – "refactoring". Well, and that it is advisable to move in small steps to make it easier.
I started coding in this style and realized that sometimes I just get stuck, not knowing what to test and how. For example, sending mail, how to test? Send yourself a letter? Basically, I tested the entire class, trying to cover each method with a little test to make sure it works. I thought, maybe it's worth not completely covering the code with tests, but only those parts where the logic is complex.

Experienced developers, advise how best to master testing, whether it is worth moving in the direction of TDD or maybe you need to wait, gain experience in testing. What should be tested and how? Perhaps there are rules that you have worked out for yourself.

Answer:

You are doing everything right – you need to write tests from the very beginning and strive for more% code coverage. What you cannot think of, how and what to test with experience will pass, but at the moment you can test how it turns out. Regarding how to test methods / classes or the functionality they implement, I can suggest that you first determine the testing methodology to begin with, look at: testing according to the white box strategy and testing according to the black box strategy .

What should be tested and how?

In my opinion and experience, all "atypical" logic should be tested first of all. That is, those specific things that you write for this application, and not that the data from the database is loaded correctly (although this also needs to be tested), or the method actually opens the file.

First of all, I try to test the most important pieces of code that, if changed, must signal to the developer (not by passing the test) that the logic of work has become different from what was expected (by the test), which should oblige the developer to update the test.

Regarding how to test – for critical sections of the code, I apply several tests at once: for functionality (black box) – I submit all possible values ​​to the input and check for compliance with the output value or throwing an exception, or the presence / absence of a side effect, and the code using coverage criteria MC / DC .

Scroll to Top