JUnit Introduction and Testing

Source: Internet
Author: User

One, JUnit introduction

Finally to the white box test, said the black box test is not standard and boring, white box testing to show our technical staff's style.

Let's define it first:

JUnit is a unit test framework for the Java language. Founded by Kent Beck and Erich Gamma, it became the most successful of the Xunit family of Sunit from Kent Beck. JUnit has its own JUnit expansion biosphere. Most Java development environments have integrated JUnit as a unit test tool. [1-3]

JUnit was written by Erich Gamma and Kent Beck.regression testFRAME (regression testing framework). JUnit testing is a programmer's test, the so-called white-box test, because the programmer knows how the software being tested (how) accomplishes the function and what it does.           JUnit is a set of frameworks that inherit the TestCase class and can be automatically tested with JUnit.           A lot of crap, but I summed up and too difficult words poor me, in short, JUnit is a unit testing framework, and unit testing is a software test in the comparison of the link between the part, simply speaking, is to write a function to test, as for the inheritance of the TestCase class, the following will be explained in detail.            Here's how to test with the introduction of JUnit in our most familiar eclipse, using code and primers (see below in WiFi). Second, code and results display

1) First create a new project called Junit_test, we write a calculator class, this is a simple implementation of the subtraction, square, the root of the class, and then to unit test these functions, we deliberately retained some bugs for demonstration, these bugs are described in the comments. This type of code is as follows:

Package andycpp;

public class Calculator ... {

private static int result; Static variables for storing run results

public void Add (int n) ... {

result = result + N;

}

public void substract (int n) ... {

result = Result-1; Bug: The correct one should be result =result-n

}

public void Multiply (int n) ... {

}//This method is intentionally empty

public void divide (int n) ... {

result = result/n;//Good function

}

public void Square (int n) ... {

result = n * n;//good function

}

public void SquareRoot (int n) ... {

for (;;);//bug: Dead Loop

}

public void clear () ... {//The result is zeroed

result = 0;

}

public int GetResult () ... {

return result;

}

}

2)

Introduce the JUNIT4 unit test package into this project: Right-click on the item, click "Properties", in the pop-up Properties window, first select "Java Build Path" on the left, then select "Libraries" tab to the right, then click "Add Library ..." on the far right. button, then select JUnit4 in the new pop-up dialog box and click OK, as shown in the JUNIT4 package is included in our project.

3)

Generate JUnit Test framework: In Eclipse's Package Explorer, right-click the pop-up menu and select JUnit test case. In the popup dialog box, make the appropriate selection, after clicking "Next", the system will automatically list the methods you include in this class, select the method you want to test. In this example, we only test the "add, subtract, multiply, divide" four methods. The system then automatically generates a new class Calculatortest, which contains some empty test cases. You only need to make these test cases slightly modified to use.

4)

The complete calculatortest 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 ());

}

}

5)

Test results: Run the test code: After modifying the code as described above, we right-click on the Calculatortest class and select "Run Asàjunit Test" to run our test.

The results of the operation are as follows:

Progress bar is red color indicates found error, the specific test results on the progress bar indicates "a total of 4 tests, of which 1 tests were ignored, one test failed."

Merit perfection, it's done.

JUnit Introduction and Testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.