Because in the program, an interface corresponds to an implementation method, and in the interface often defines a number of related methods, so at the time of testing, if all in the main method inside the test, the main method will appear bloated, and it is not easy for other people to test and view the test data, with JUnit Test, a method corresponding to a test method, simple and clear, but also easy for others to view the test method and test data.
If you have more than one method in your class, it is inconvenient to test it with the main method, so if you want to test all the methods, you have to write the test code all in main, or you can test a rewrite once. And more importantly, this will make the test code mixed with running logic code, not canonical.
In a regular Java project (especially the use of a framework such as SSH), almost will not write the main method, write is useless code, will be scolded by the manager ...
Using JUnit is a lot easier, this is unit testing, which method you want to write a corresponding test method, and then run with JUnit. Each method is independent and very flexible. And the test method is generally not written directly in the original class, but a separate test class, so that the test code is completely separate from the logic code.
If you use tools like Maven to manage your projects, the benefits of JUnit are further reflected: you can write a large number of test classes, and then use a simple maven command to automate, think about, all automated testing, and test results automatically generated documents, convenient.
In fact, JUnit is not difficult to learn, with one or two times on the general understanding. I wish you a smooth study.
Why use JUnit test instead of the normal Java Main method to complete the test?