The use of junit [required for java unit testing]

Table of contents

The concept of unit testing

Junit unit testing framework

Advantages

Junit test sample

Common annotations for unit testing


The concept of unit testing

Unit testing refers to the work of checking and verifying the smallest testable unit in software in isolation from other parts of the program, where the smallest testable unit usually refers to a function or a class .

The principles of unit testing are:

  • When testing code, only consider the test, not the internal implementation;
  • The data simulates reality as much as possible, the closer to reality the better.
  • Fully consider the boundary conditions of the data
  • For key, complex, core code, and key tests
  • Use AOP (aspect-oriented programming) to reduce test code and avoid useless functions
  • The combination of testing and functional development is conducive to design and code refactoring

Junit unit testing framework

JUnit is a unit testing framework implemented in the Java language. It is open source, and Java developers should learn and use JUnit to write unit tests.

In addition, almost all IDE tools integrate JUnit , so that we can write and run JUnit tests directly in the IDE . The latest version of JUnit is 5

Advantages

  • A series of test methods can be written 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 look at 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 called automatically. No need to add additional call statement.
  • Adding, deleting, and shielding test methods do not affect other test methods. Open source frameworks have corresponding support for JUnit

Junit test sample

step

1. Import Junit jar package

2.  Write a test method: The test method must be a public non-static method with no parameters and no return value.

3.  Use the @Test annotation on the test method : mark the method as a test method

4. Run to test

Code example:

main program

/**
 * @author Candice
 */
public class UserService {
    public String loginName(String loginName, String passWord){
        if("tong".equals(loginName) && "123456".equals(passWord)){
            return "登陆成功";
        }
        else{
            return "用户名错误or密码有问题";
        }
    }

    public void selectName(){
        System.out.println((10 / 2));
        System.out.println("查询成功");

    }
}

unit test program

import org.junit.Assert;
import org.junit.Test;

/**
 * @author candice
 */
public class TestUserService {

    @Test
    public void testLoginName(){
        UserService userService = new UserService();
        String rs = userService.loginName("tong","123456");

        // 进行预期结果的正确性测试: 断言
        Assert.assertEquals("你的功能业务可能出现问题", "登陆成功", rs);
    }

    @Test
    public void testSelectName(){
        UserService u = new UserService();
        u.selectName();

        // 不需要断言,因为selectName 没有返回值 Assert.assertEquals("没问题",null,);
    }


}

Then run test file

 If the test sample even read passed, then

ok, the simplest unit test example has been implemented.

Common annotations for unit testing

 Go ahead and explore.

Guess you like

Origin blog.csdn.net/candice5566/article/details/125750205
Recommended