JUnit Unit Test Basics Essentials

Source: Internet
Author: User

 junit Unit Test Basics Essentials

1.JUnit is a framework for testing code that is designed to ensure that the code is correct, not the code.

2. The test class is generally not put together with the target class, but the compiled class file is put together.

3. Unit testing is mainly to test the results of the program and its expected value is equal

4. steps to create a test class :

1) Under Project, create a source folder named Test, which is the same class as the SRC folder.

2) The package name of the test class and the target class should be the same , the advantage is that the test class does not import the target class source code

3) test class naming convention: If the name of the target class is Calculator, the test class created should be named Calculatortest or Testcalculator

The 5.JUnit test framework is currently broadly divided into two categories (JUnit 3.8 and JUnit 4.x)

———————————— JUnit 3.8 ———————————————

The package involved is generally import junit.framework.*

6. In JUnit 3.8, the test class inherits the Testcast class, and the Testcast class inherits from the Assert class , and the test method needs to meet the following principles:

1) Modify subscript character to public

2) return type void

3) No method parameters

4) method name must start with test

7. Cannot rely on the execution order of test methods

The 8.setUp method and the Testdown method are all methods of the TestCase class.

1) TheSetup method is performed prior to executing each test method

2) TheTestdown method is performed after executing each test method.

———————————— JUnit 4 ———————————————

The package involved is import org.junit.*

9) in JUnit 4 The test class does not have to inherit from the TestCase class, the test method name does not need to start with test, mainly in the form of annotations defined , as long as the test method plus @Test Can be tested, but some of the principles are to be designed in junit3 style, such as testing method to start with test, habit problem,

10)

  • @Test: Defining a flag for a test method
  • @Test (timeout=1000) : Set timeout time , test fails if test time exceeds timeout defined by you
  • @Test (Expected): declare anomalies that occur , such as @Test (expected = exception.class)
  • @Before: The same effect as the Setup () method on JUnit 3.8, where the method name is better than setup (), but is not forced to be executed before each test method
  • @After: The same effect as the teardown () method on JUnit 3.8, where the method name is best and teardown (), but not forced, is executed after each test method
  • @BeforeClass: The method declared by the annotation, which is performed before all test methods, only once
  • @AfterClass: The method declared by the annotation, which is performed after all test methods and executed only once
  • @Ignore: Let the test method or test class not be executed, let it lose the function of testing

Summary:

If there are 8 test methods in a test class, then each test method needs to create a test class object, and each object will only invoke a test method ( in order to conform to the conventions of the command pattern ), creating a total of 10 test class objects.

Before adding a test method, you need to make some judgments about the test method:

1) Modify subscript character to public

2) return type void

3) No method parameters

4) method name must start with test

If the above requirements are met, they are added to the collection as a test method to perform

JUnit Unit Test Basics Essentials

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.