"Go" using JUNIT4 for unit testing in Eclipse (beginner)

Source: Internet
Author: User

Http://www.builder.com.cn/2007/0901/482336.shtml

First, let's take a stupid crash tutorial and don't ask why, follow Me, let's experience the thrill of unit testing!

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 calculator class, and then to unit test these functions. This class is not perfect, and we deliberately keep some bugs for demonstration, which 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 has not been written well
public void divide (int n){
result = result/n;
}
public void Square (int n){
result = n * n;
}
public void SquareRoot (int n) {
for (;;); Bug: Dead Loop
}
public void Clear () {//results are zeroed
result = 0;
}
public int GetResult () {
return result;
}
}

The second step is to 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 the "Libraries" tab on the right, then on the far right, click on the "Add Library ..." button as shown:


Then select JUnit4 in the new pop-up dialog and click OK, as shown in the JUNIT4 software package is included in our project.

The third step is to generate the JUnit test framework: In Eclipse's Package Explorer, right-click on the pop-up menu and select "Newàjunit test Case". As shown in the following:


In the popup dialog box, make the appropriate selections as shown in:


After clicking "Next", the system will automatically list the methods contained in your class and choose the method you want to test. In this example, we only test the "add, subtract, multiply, divide" four methods. As shown in the following:


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. 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 (ten);
        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 ());
    }
}

Fourth step, 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 as shown in:

The results of the operation are as follows:

The 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, a test failed"

At this point, we have fully experienced the methods of using JUnit in Eclipse.

"Go" using JUNIT4 for unit testing in Eclipse (beginner)

Related Article

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.