当前位置:网站首页>JUnit unit test

JUnit unit test

2022-07-06 17:20:00 bestkasscn

Junit unit testing

brief introduction

JUnit It's a Java Unit testing framework for programming languages .JUnit There are important developments in Test Driven Development , It originated from JUnit One of them is called xUnit One of the unit testing frameworks of .

Junit It's an automated testing framework , It can help developers unit test interfaces and methods , Just check the final result to know whether the interface or code of the whole project is smooth

The import method

  • Use maven Import
    stay maven Of <dependencies></dependencies> Add the following code
<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
</dependency>
  • Use official jar package

Usage method

First of all we have src.main.java Create a directory Calculator class

public class Calculator {
    
    public int add(int a,int b){
    
        return a + b;
    }
    public int sub(int a,int b){
    
        return a - b;
    }
    public int div(int a,int b){
    
        return a / b;
    }
    public int mul(int a,int b){
    
        return a * b;
    }
}

And then src.main.java Create a directory CalculatorTest class , We are here to sub Method as an example sub Method

import com.bestkasscn.CatCatProject.Calculator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
    
    @Test
    public void testDiv(){
    
        Calculator calculator = new Calculator();
        int result = calculator.sub(1, 2);
        Assertions.assertEquals(-1,result);
    }
}

In the class, we define a testDiv() Method , This method is the test method , We need to add @Test annotation , It means this is a test method , And the method requires null parameters , No return value , Method name optional , But try to be related to the method you want to test
The verdict :
* Red : Failure
* green : success
* Generally, we use assertion operations to process the results
* Assertions.assertEquals( Expected results , Result of operation );

 Insert picture description here

After writing the test method , Click the green button on the left side of the method to start the test
The test results are as follows
 Insert picture description here

Green means passing .

summary

*  step :
	1.  Define a test class ( The test case )
		*  Suggest :
			*  Test class name : The name of the class being tested Test		CalculatorTest

	2.  Define test methods : Can run independently 
		*  Suggest :
			*  Method name :test Test method name 		testDiv()  
			*  Return value :void
			*  parameter list : Empty ginseng 

	3.  Add to the method @Test
	4.  Import junit Depend on the environment (maven or jar package )

*  The verdict :
	*  Red : Failure 
	*  green : success 
	*  Generally, we use assertion operations to process the results 
		* Assertions.assertEquals( Expected results , Result of operation );

*  Add :
	* @Before:
		*  Modified methods are automatically executed before testing methods 
	* @After:
		*  The modified method is automatically executed after the test method is executed 
原网站

版权声明
本文为[bestkasscn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060928273112.html