JUnit unit test (1) -- JUnit Introduction

Source: Internet
Author: User

JUnit unit test (1) -- JUnit Introduction

1. JUnit Introduction

JUnit is a Java testing framework for source code development. It is used to write and run reusable tests. It is an example of xUnit, a unit testing framework system (for java ). It is mainly used for white box testing and regression testing.

1.1 JUnit benefits and JUnit testing principles

1.1.1 benefits of using JUnit:

The test code can be separated from the product code.

The test code for a class can be applied to the test of another class with few changes.

It is easy to integrate into the construction process of testers. The combination of JUnit and Ant can implement incremental development.

JUnit is open source code and can be used for secondary development.

You can easily expand JUnit.

1.1.2 JUnit test writing principles:

Simplify the compilation of tests, including the learning of the test framework and the compilation of actual test units.

Makes the test unit persistent.

You can use existing tests to compile relevant tests.

1.2 JUnit features

1.2.1 The APIS provided allow you to write reusable unit test cases with clear test results

1.2.2 provides three methods to display your test results, and can be expanded.

1.2.3 provides the unit test case batch operation function

1.2.4 ultra-lightweight and easy to use, without commercial spoofing and useless wizard

1.2.5 the entire framework is well designed and easy to expand

1.3 JUnit framework composition

1.3.1 a set of methods and processes for testing the test target, which can be referred to as a test case (TestCase ).

1.3.2 a set of test cases, which can contain multiple test cases (TestCase) called test suite ).

1.3.3 description and record of test results. (TestResult ).

1.3.4 Test Event listener (TestListener ).

1.3.5 description of the inconsistency between each test method and expectation, that is, its test failure element (TestFailure)

1.3.6 AssertionFailedError ).

1.4 common interfaces and classes in JUnit

1.4.1 Test interface: Run and collect Test results

The Test interface uses the Composite design mode. It is a common interface for separate Test cases (TestCase), aggregation Test mode (TestSuite), and Test extension (TestDecorator.

Its public int countTestCases () method is used to count the number of TestCase during the test.

The other method is public void run (TestResult). TestResult is the test result of the instance. The run method runs this test.

1.4.2 TestCase abstract class: Define a fixed method in the test

TestCase is an abstract Implementation of the Test interface (it cannot be instantiated but can only be inherited). Its constructor TestCase (string name) creates a Test instance based on the input Test name. Because each TestCase must have a name when it is created, if the test fails, you can identify which test failed.

The setUp () and tearDown () methods contained in the TestCase class.

In the setUp () method set, all variables and instances required for the test are initialized, And the setUp () method is executed again before each test method in the test class is called in turn.

The tearDown () method releases the variables and instances referenced in the test procedure method after each test method.

When writing a test case, you only need to inherit TestCase to complete the run method. Then, JUnit obtains the test case, executes its run method, and records the test results in TestResult.

1.4.3 Assert static class: a set of assertion Methods

Assert contains a set of Static Test Methods for comparing the expected value with the actual value. That is, if the test fails, the Assert class throws an AssertionFailedError exception, the JUnit testing framework classifies this error as Failes and records it, and marks it as failing the test.

If a parameter of the String type is specified in this method, this parameter is used as the identifier of the AssertionFailedError exception, telling the tester to modify the exception details.

JUnit provides six types of 31 groups of assertions, including basic assertions, digital assertions, character assertions, Boolean assertions, and object assertions.

The following table lists the eight core methods provided by the Assert superclass.

AssertTure
The asserted condition is true. If not, AssertionFaildeError with the corresponding information (if any) is thrown.
 
AssertFalse
The assertion condition is false. If not, AssertionFaildeError with the corresponding information (if any) is thrown.
 
AssertEquals
Assertion two objects are equal. If not, AssertionFaildeError with the corresponding information (if any) is thrown.
 
AssertNotNull
The assertion object is not null. If not, AssertionFaildeError with the corresponding information (if any) is thrown.
 
AssertNull
The assertion object is null. If not, AssertionFaildeError with the corresponding information (if any) is thrown.
 
AssertSame
Assertion two references point to the same object. If not, AssertionFaildeError with the corresponding information (if any) is thrown.
 
AssertNotSame
Assertion two references point to different objects. If not, AssertionFaildeError with the corresponding information (if any) is thrown.
 
Fail
The test fails and the specified information is displayed.
 

1.4.4 TestResult result class and other classes and interfaces

The TestResult result class combines any accumulated test results and passes each test Run () method through the TestResult instance. If the execution of TestCase fails, TestResult will throw an exception.

The TestListener interface is an event listening protocol and can be used by the TestRunner class. It notifies listener of Object-related events, including startTest (Test test), endTest (Test test), error, and addError (Test test, Throwable t) exceptions) and add failed addFailure (Test test, AssertionFailedError t ).

The TestFailure failure class is a collection class that describes exceptions during each test execution. Its toString () method returns a brief description of the "failure" status.

Share:

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.