Use junit4 in eclipse for unit testing (I)

Source: Internet
Author: User
Abstract: This article describes how to use junit4 for unit testing in eclipse.

First, let's take a silly quick tutorial. Don't ask why, follow me. First, let's take a look at the pleasure of unit testing!

First, create a project named junit_test. We will compile a calculator class, which is a calculator class that can easily implement addition, subtraction, multiplication, division, square, and square, and then perform unit tests on these functions. This class is not perfect. We intentionally reserved some bugs for demonstration. these bugs are described in comments. The Code is as follows:

Package andycpp;
Public class calculator ...{
Private Static int result; // static variable used to store running 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
Public void divide (int n )...{
Result = Result/N;
}
Public void square (int n )...{
Result = N * N;
}
Public void squareroot (int n )...{
For (; // BUG: endless loop
}
Public void clear ()... {// clear the result
Result = 0;
}
Public int getresult ()...{
Return result;
}
}

Step 2: Introduce the junit4 unit test package to this project: Right-click the project and click "properties ",

In the pop-up property window, select "Java build path" on the left, select the "Libraries" tab on the top right, and then click "add Library…" on the rightmost side ..." As shown in:

In the displayed dialog box, select junit4 and click OK, as shown in. The junit4 software package is included in our project.

Step 3: generate the JUnit test framework. In the eclipse package explorer, right-click the class pop-up menu and select "New à JUnit test case ". As shown in:

In the pop-up dialog box, make the appropriate selection, as shown in:

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

The system will automatically generate a new class calculatortest, which contains some empty test cases. You only need to modify these test cases. 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());
}
}

Step 4: run the test code. After modifying the code, right-click the calculatortest class and select "Run as à JUnit test" to run the test. The result is as follows:

The progress bar is red to indicate that an error is found. The specific test result is displayed on the progress bar, which indicates that "four tests have been performed. One test is ignored and the other test fails ".

Now, we have completely experienced how to use JUnit in eclipse.

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.