JUnit unit tests in Java

Source: Internet
Author: User
Tags array length mul

JUnit Meta Test

Unit Testing (JUnit testing) is the inspection and validation of the smallest testable unit in the software. A module in Java refers to a class.

JUnit, an open source Java Unit Test Framework, is a standard unit test library for Java, a very important Third-party Java library, developed by Kent Beck and Erich Gamma.

JUnit as a software testing tool, JUnit can not break Java original code, easy to use, add fast, less code, visualization of the test.

JUnit Unit Test steps:

1. Import Package JUnit Test Package: JUnit test version, 3.81 version, 4.0 version, import the corresponding jar package;

2. Write a class extension (inheritance) TestCase;
3. Add test to generate new testing method before the name of the method that needs to be tested;
4. Run the test, use the assertion (assert***) method to test for success (show green) or fail (show red), or determine whether you are right.

The difference between Unit3.81 and JUnit4.0:
1. In JUnit 4, tests are identified by @Test annotations;
2.Unit 3.81 the test run program automatically invokes the Setup () method before running each test, in JUnit4.0, with @Before annotations;
3. In JUnit 3.81, tests run programs automatically use the Teardown () method to clear the consumed resources before running each test, and in JUnit4.0, @After annotations.

Note: If the method you want to test does not return a value, you can only test its procedure.

JUNIT4 new Features:
1. The SetUp () method and the Teardown () method that are part of the class scope will run once before the test method in the class is run, and any method that uses the @BeforeClass annotation is @AfterClass The method of the annotation will run once after all the tests in the class have been run;
2. Exception test: Write the code that throws the exception, and use the annotation to declare that the exception is expected;
3. In the need for efficient testing, some code tests you think you can skip, such tests can be annotated as @Ignore

4. The time test can be annotated with the timeout parameter, and the test fails if the test runs longer than the specified number of milliseconds.

5. Add two new assertion methods:

(1) public static void Assertequals (object[] expected, object[] actual)
(2) public static void Assertequals (String message, object[] expected, object[] actual) These two methods are used to compare arrays: If the array length is equal and the corresponding element is the same, The two arrays are equal, otherwise, and the array is considered empty.

JUnit Class (Mul) unit tests:

Package com.myhome;

public class mul{Public
int Mul (int x,int y) {public
    int Mul (int x,int y) {return
        x*y;
    }
    Public double divide (double x,double y) {return
        x/y;
    }
}

Add jar packs and JUnit test support with the MyEclipse integrated development environment:

1. If not, just the bottom of the new menu ... Medium (or CTRL + N), enter JUnit Select the JUnit test Case Unit test:


2. Choose JUnit 3 Test tests:


3. Select the method to test:


4. The jar package (JUNIT3 or JUNIT4 need) is automatically imported at this time.


5. Program Directory structure:

6. The test class at this time is:

Package com.myhome.test;

Import Junit.framework.TestCase;

public class Multest extends TestCase {public

    void Testmul () {
        fail ("not yet implemented");

    public void Testdivide () {
        fail (' not yet implemented ');
    }

To test the internal method body:

    public void Testmul () {
        Mul Mul = new Mul ();
        int res = Mul.mul (2, 4);
        System.out.println (res);

    public void Testdivide () {
        Mul Mul = new Mul ();
        Double res = mul.divide (8.0, 2.0);
        Assertequals (4.0, res);
    

Effects chart:

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.