Unit tests (JUnit use)

Source: Internet
Author: User

1. The object being tested is a method in a class.

2. Import the jar package.

3, Unit testing method, the test method naming rules for the public void Method name () {}, Note: The test class cannot be named public class Test{},test and @Test names will be reported Type Mismatch:cannot Co Nvert from Test to Annotation

Create the source folder folder, test the package name of the class and the package name of the method being tested

4, the use of annotations to run the test method, the test method is added above @Test

5. Choose the test method name right-click Run As→junit test.

The code is as follows:

method to be tested:

1 Package Com.junit; 2 3  Public class Junit {4      Public void sum (int A,int  b) {5         System.  out. println (A +b); 6     }7 }

Test method:

1  PackageCom.junit;2 3 Importorg.junit.Test;4 5  Public classTest1 {6 @Test7      Public voidtest1 () {8JUnit JUnit =NewJunit ();9Junit.sum ();Ten     } One}

For example, a green bar indicates a successful unit test, and a red bar indicates a failure

@Ignore: Indicates that unit tests are not run

@Before: Run Before method

@After: Run After method

Assertion:

Assert Assertequals (expected, actual value).

  

Unit tests (JUnit use)

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.