What is the difference between using the JUnit class and using the console to test?

0

I have used JUnit several times, however, the question I have is, what is the difference between using JUnit and showing something by console?

Let's give 1 example, if I wanted to know if I ordered a number arrangement well, the JUnit with the assert methods will tell me if it is correct, but at the same time if I show that arrangement by console, I will also see if it is correct or not, then, why not always use the console as a test or test method? What does JUnit do that in the books and in the classes I mention it as super useful?

    
asked by Christian H 08.10.2018 в 00:54
source

2 answers

1

Automation, repeatability, "atomization" and integration in the build and packaging process.

If you are going to do a program, try to work it out and forget about it (for example, a practice for a teacher), JUnit does not give you much. It is a simple program with few specifications; You look that he meets them, you deliver the program and something else.

But in the real life of a professional programmer, you write a code that is part of a complex system and after a few months or many years you have to modify it, and if you modify it you have to prove that no bugs have been introduced that make you lose money to your company, and here JUnit gives you:

  • Automation: The tests are executed automatically, you do not have to do them by hand checking results from screen (that apart from being slow, it can be misleading).

  • Repeatability: The same tests are performed each time. In addition, you do about the final version of the class. If you do the test by printing values by console and then delete those messages from your program, you are making a change in your program that you will not check with your test.

  • Atomization: Each small module (ideally each class) is tested separately. This allows us to test more thoroughly all the details of the operation of the class. If you have a complete system, testing the operation of all possible cases is much more difficult or even impossible. Also, when you enter a bug in a class, ideally you will detect it in the test of that class, which is usually easier to fix.

  • Integration: Tests are usually part of the project's build and packaging process; If they fail, the process is stopped so that the bugs have to be corrected before continuing. They are not a step that a rushed programmer can forget or do too quickly.

  • They go with the code: They are not a document of manual tests that still have not been updated.

It also has drawbacks:

  • You have to write the tests and, if the functionality of the class changes, update them.

  • They are not a panacea. The tests will test what you tell him to try. If some combination is not tested and when making a change that combination causes a bug, it will not be detected.

  • Focuses on unit tests . In theory it is to test the operation of a small module (typically a class) isolated from the others. That makes the tests simpler, but it means that you then have to test the overall performance ("by hand" or with other automated test environments oriented to system tests).

    • In this case, the advantage of JUnit is that it allows you to test each class more thoroughly at the beginning of the build process; a system test is much slower and it is often not easy to modify the system to simulate special conditions.
answered by 08.10.2018 в 03:21
-1

JUnit is a set of libraries that allow you to test your programs, the reason why it is very useful is that although in programs of great simplicity such as the ordering of an array it might seem quite useless or inefficient The truth is that programs of a medium / large size is very useful because it saves you a lot of time thinking about a personalized test to test all your classes and created objects, as well as if your algorithms return the expected values among other things without Keep in mind that if you change something in your program, most likely you will have to modify one or several lines of code in your test and in the long run it is very cumbersome and expensive. On the other hand JUnit gives you the possibility of having an initial template with several methods to create your objects and by using assert methods to test what you need, this is very easy since in the setUp() method all your objects are started and in the methods with the @test tag you can test your classes, methods and variables. On the other hand with JUnit you can test if a certain exception that may be waiting for you is produced (or not). There are many other tools and options that allows you to implement JUnit in your programs. You can see more about JUnit in this page link and you even have a "User Guide" that will help you in your doubts and use of the tool. I hope I have settled your doubt. Greetings.

    
answered by 08.10.2018 в 02:18