JUNIT-java unit test

When writing large programs, you need to write thousands of methods or functions, these functions may be very strong, but we only use a small part of the function in the program, and can be determined after commissioning, this small Some features are correct. But, at the same time we should ensure that every function is completely correct, because if in the future if we extend the program, use the other functions of a function, and this function has a bug, then it is definitely a very depressing thing. So, after each finished writing a function that should be tested every aspect of this function, this test is called unit testing. Traditional programming, unit testing is a very troublesome thing, you have to re-write another program, you need to call a method tested in this program, and carefully observe the operation results to see if there is wrong. Because of this trouble, so passionate programmers write unit tests is not very high. So there is a cow who launched the unit test package, which greatly simplifies the task of unit testing to do, which is JUnit4. This paper briefly describe the method JUnit4 Eclipse3.2 in unit testing.

First put two links

Mu class network - video

W3Schools - Documents

Creating a

First create a new project called JUnit_Test, we write a Calculator class, which can be a simple implementation of arithmetic, square, square root calculator class and unit test these features. This class is not perfect, we have deliberately kept some for demonstration Bug, the Bug has a description in the comments. This class code is as follows:


andycpp Package;

public class the Calculator  {
    Private static int Result; // static variables for storing operation results
    public void the Add (int n-)  {
        Result = Result + n-;
    }
    public void Substract (n-int)  {
        Result = Result -. 1 ; // Bug: right should be-n-Result = Result
    }
    public void Multiply (n-int)  {
    } // this method has not been written
    public void Divide (int n-)  {
        Result = Result / n-;
    }
    public void Square ( n-int)  {
        Result = n-n-*;
    }
    public void the squareRoot (n-int)  {
        for (;;); // the Bug: infinite loop
    }
    Public void Clear ()  {// clears the result
        Result = 0;
    }
    public int the getResult ()  {
        return Result;
    }
}

Two import

A second step of introducing the test pack unit JUnit4 item: right click on the item, point "Properties", as shown:



Properties in the pop-up window, first select the left "Java Build Path", to the upper right and then select the "Libraries" tab and then on the far right click "Add Library ..." button, as shown below:


Then in the new dialog box select JUnit4 and click OK, as shown above, JUnit4 package was included into our project.

Three generation 

The third step is to generate JUnit testing framework: in with the Eclipse Package Explorer, right-click on the type of pop-up menu, select "New à JUnit Test Case". As shown below:


In the dialog box, be selected accordingly, as shown below:


    After clicking "Next", the system automatically lists the way you included in this class, the method you want to test. In this example, we only "add, subtract, multiply, divide," four test methods. As shown below:

After the system automatically generates a new class CalculatorTest, which contains a number of empty test. You only need to use these test cases can be slightly modified. CalculatorTest complete code is as follows:


package andycpp;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class CalculatorTest  {

    private static Calculator calculator = new Calculator();
    
    @Before
    public void setUp() throws Exception {
        calculator.clear();
    }

    @Test
    public void testAdd() {
        calculator.add(2);
        calculator.add(3);
        assertEquals(5, calculator.getResult());
    }

    @Test
    public void testSubstract() {
        calculator.add(10);
        calculator.substract(2);
        assertEquals(8, calculator.getResult());
    }

    @Ignore("Multiply() Not yet implemented")
    @Test
    public void testMultiply() {
    }

    @Test
    public void testDivide() {
        calculator.add(8);
        calculator.divide(2);
        assertEquals(4, calculator.getResult());
    }
}

四  运行

 

第四步,运行测试代码:按照上述代码修改完毕后,我们在CalculatorTest类上点右键,选择“Run As à JUnit Test”来运行我们的测试,如下图所示:

运行结果如下:


 

进度条是红颜色表示发现错误,具体的测试结果在进度条上面有表示“共进行了4个测试,其中1个测试被忽略,一个测试失败”

Guess you like

Origin www.cnblogs.com/bomily0212/p/12160704.html