Junit unit testing

Junit

1 What is Junit

JUnit is a unit testing framework for the Java programming language. JUnit has made a very important development in test-driven development. It is one of the unit testing frameworks collectively called xUnit that originated from JUnit.

2 Unit testing concepts

  • Unit: In Java, a class is a unit
  • Unit test: A small piece of code written by a programmer to perform functional testing or business logic testing on a method in a class.

3 The role and benefits of Junit unit testing framework

It is used to conduct purposeful testing of the method functions in the class to ensure the correctness and stability of the program.
Allows methods to run independently.
benefit:

You can write a series of test methods to unit test all interfaces or methods of the project.
After startup, the test is automated and the execution results are judged without human intervention.
You only need to check the final result to know whether the method interface of the entire project is smooth.
Each unit test case is relatively independent, started by Junit, and automatically called. No need to add additional calling statements.
Adding, deleting, and blocking test methods does not affect other test methods. Open source frameworks all have corresponding support for JUnit.

4 Steps to use Junit unit testing framework

Write business classes and write business methods in business classes. For example, the method of adding, deleting, modifying and checking
Write a test class, write a test method in the test class, and write test code in the test method to test.
Naming convention for test classes: start with Test, end with business class name, use camel case naming method
The first letter of each word is capitalized, which is called camel case naming method, such as class names, interface names…
Capitalize the first letter starting from the second word, which is called camel case naming, such as method naming
For example, if the business class name is: ProductDao, then the test class name should be: TestProductDao
Naming rules for test methods: start with test and end with the business method name
For example, if the business method name is: save, then the test method name should be: testSave

5 Notes on test methods

  • Must be public modified, no return value, no parameters
  • The annotation @Test must be modified

6 How to run test methods

  • Select the method name –> right-click –> Run ‘test method name’ to run the selected test method
  • Select the test class name –> right-click –> Run ‘test class name’ to run all test methods in the test class
  • Select the module name –> right-click –> Run ‘All Tests’ to run all test methods of all test classes in the module

7 How to view test results

  • Green: indicates the test passed
  • Red: Indicates that the test failed and there is a problem

8 Junit common annotations (Junit4.xxxx version)

* @Before: Used to modify the method, which will be executed once before each test method is executed.
* @After: Used to modify the method, which will be executed once after each test method is executed.
* @BeforeClass: Used to statically modify the method, which will be executed once before all test methods.
* @AfterClass: Used to statically modify the method, which will be executed once after all test methods.

9 Junit common annotations (Junit5.xxxx version)

@BeforeEach: Used to modify the method, which will be executed once before each test method is executed.
 @AfterEach: Used to modify the method, which will be executed once after each test method is executed.
 @BeforeAll: Used to statically modify the method, which will be executed once before all test methods.
 @AfterAll: Used to statically modify the method, which will be executed once after all test methods.

10 Use of Junit

  • Sample code:
//Generally speaking, the main method is created and the main method can be executed.

public class Demo01 {
    public static void main(String[] args) {
      new Demo01().add();
    }
    public static void add(){
        int a=5;
        int b=10;
        System.out.println(a+b);
    }
}
   //After adding @test, you can directly click to run or debug
    @Test
    public  void add(){
        int a=5;
        int b=10;
        System.out.println(a+b);
    }

Demo video:

Related Posts

From shallow to deep, I will take you through the three ways to implement array expansion in Java [recommended collection]

Java connects to the database to implement basic additions, deletions, modifications and queries

[JavaSE] Multi-threading (3) User threads, daemon threads and seven major states of threads

When adding dependencies to pom files, an unknown problem occurs [maven]. A complete solution to dependency error reporting in maven projects.

Usage of Qt QTableWidget table control (very detailed)

A simple tutorial on how to use the MybatisX plug-in in SpringBoot (super detailed!!)

Windows 11 configure JDK environment variables

append() method in JAVA

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*