What is JUnit?

JUnit is a test vec form for java language. This verk form is used to test a small part of the program and project. I am Arslan Mirbzergi,in this article I need to remind you of a few tips for using JUnit to your dear companions and readers.


Testing the program at each stage causes errors, productivity and stability of the written code. Many tools have made it possible for us to test our code. JUnit is a tool that makes it possible for us.

Training work with JUnit:

1- First create your classes in project-name/src/test/java path.
2- After creating a new class, it is enough to add SpringBootTest@ to the class.
3- In the next step, add the Test@ to specify the test methods in the class.
4- To ensure that a method is performed in a class such as service or repository or any other class, we need to do Autowired@. This will test the desired method.
5- To test the methods in JUnit, you can use the static methods in the Assert class such as AssertEquals, AssertTrue, etc. Use.
6- If you add Before@ on a method, the method will be performed once before each method at the time of the class test.
7- After@ acts like Before@, except that the run of that method is done after the run of each method.
8- Adding BeforClass@ and AfterClass@ also allows the test to be performed before or after the method.


In the image below, you will see an example of the Java class. In this example, Test@ is used to determine the method requested for testing.