[Unit testing and JUnit 4] Is the traditional main method testing code too troublesome? Let's try JUnit4

foreword

As we all know, when we write programs again, we cannot do without the step of testing and debugging. No programmer can guarantee that he will never make mistakes.

We generally test immediately after writing a piece of code, so as to prevent errors from being found because too many codes are involved and which piece of code is wrong. Then we need unit testing.

Table of contents

unit test

Unit testing refers to checking and verifying the smallest testable unit in software. For the meaning of a unit in unit testing, generally speaking, its specific meaning should be determined according to the actual situation. For example, in C language, a unit refers to a function, in Java, a unit refers to a class, and in graphical software, it can refer to a window or a menu. wait.

To briefly summarize:

(1) Unit testing refers to 最小可测试单元the inspection and verification of the software;
(2) Test cases refer 编写一段代码to 已有功能(方法)the verification of the software;

Generally speaking, in Java, we perform unit testing by writing the main method and then creating the object that needs to be called in the main method, and then calling its method.

In small projects, this method can barely make do, but if the project is relatively large, it will be more cumbersome.

Don't be afraid. In order to facilitate our development, programming masters thought of packaging a series of unit testing tools for our use.

JUnit 4 is the most famous unit testing tool in Java, and mainstream IDEs have built-in support

JUnit 4

How to use Junit 4:

(1) Introduce the JUnit jar package or add Maven dependencies
(2) Write test cases to verify that the target method runs correctly
(3) Add the @Test annotation to the test case to start unit testing

Demo:

Because our current development is almost always the development of creating a Maven project, then we directly create a Maven project for demonstration.

This is the basic directory of a maven project.
insert image description here
First, we introduce maven dependencies in pom.xml.

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
		<scope>test</scope>
	</dependency>
</dependencies>

Write a simple test case in the src/main/java directory
insert image description here

public class Calculator {
    
    
    public int add(int a, int b) {
    
    
        return a + b;
    }
    public int subtract(int a, int b) {
    
    
        return a - b;
    }
}

Create a test class in the src/test/java directory to write a test case to verify that the target method is running correctly, and add the @Test annotation to the test case to start the unit test.

public class CalculatorTest {
    
    
    private Calculator cal = new Calculator();
    //1.与原方法保持一致
    //2.在原方法前增加test前缀
    @Test
    public void testAdd() {
    
    
        int result = cal.add(1, 2);
        System.out.println(result);
    }
    @Test
    public void testSubtract() {
    
    
        int result = cal.subtract(1, 2);
        System.out.println(result);
    }
}

We write two test cases here.
insert image description here
There are small green triangles on the left, and we can run these methods separately. If it is the small green triangle of the entire test class, it is to run all the test methods.

operation result:
insert image description here

epilogue

The use of JUnit 4 is so simple, and the practicability is also very strong.

Guess you like

Origin blog.csdn.net/apple_51673523/article/details/124419927