📜 ⬆️ ⬇️

Java programming training with automated tests

It all started with a simple - one friend of mine wanted to write programs in Java. Naturally, just learning the syntax of the language, you will not get any experience in programming, and therefore you just need to write at least something. So I pondered the question of what is best suited for this. I came to the conclusion that the best thing at the beginning of the path, in my opinion, is to create a simple class that would describe an abstract "entity", but at the same time it was easy to understand. As a result, it was decided to issue a training task for writing a self-made class of a complex number.
Of course, it would be possible to simply write on the leaflet all the requirements for this class, describe the interface of all its methods, and the like, but then it would have to be checked, and it is very likely that it would be checked several times, since a comrade is just learning.
But here I was drawn to the side of the experiments, and since I hadn’t been interested in TDD for a long time, the experiments went to the same steppe.


And so, I thought, why not take, and arrange the task as a set of tests, which would first, speed up the development process, because in case of an error, the reason is immediately displayed on the screen, and all you need is to show imagination and edit the code so that the cause of the error is corrected, and secondly, it will allow to step-by-step complicate the program, while controlling each step and result.
Plus, in this way, it is possible to graft the rules of good tone, by type, use the getValue / setValue methods instead of using public fields simply by controlling the existence and correctness of these methods with tests.

And now it's time to code. The first thing I decided was to carry out all the tests inside the main function through normal method calls. What is the reason for this? Everything is simple - the main task is the step-by-step creation of a class, with a gradual increase in its complexity, which is very easy to arrange by specifying the sequence of calls to the testing methods and exiting the program in case one of them fails. Also, in order to find out what the test results are, it is enough just to press F6 (I did it in the netbins), find out from the output window what exactly is wrong, and continue editing the code.
')
I decided to build this simulator in the form of a “sequence of actions”, as a result of which a class of a complex number is created independently and its methods are implemented. All this is realized in the form of automatic tests that check the presence of a class, then the presence of the necessary methods, then they begin to check the results of the work of these methods, and so on.

The result of all this was the application, "coaching" a novice in the creation of small classes. At once I will make a reservation that knowledge of the syntax of a language is implied, at least at the level of writing valid constructs.
Of course, you cannot grow a programmer with one such application, but for beginners this way of learning can be quite a good indication of where to go further.

For everyone who wants to try out for themselves, what happened in the end, I suggest downloading the archive with the project (for netbins). Terms of use are simple:
-Open the project
-Open the doxygen / html / index.html file (it contains explanations of the requirements and some useful information, as well as descriptions of everything under the hood)
- Execute the application and follow the instructions in the output until all tests are completed.
In fact, all this should take about an hour.
I will be glad to constructive criticism.

Lastly, the question - does such training make sense and will there be any effect from it?

Source: https://habr.com/ru/post/232055/


All Articles