Implementing Java unit Tests with the JUNIT framework

Source: Internet
Author: User

With the gradual increase of software project, software testing is becoming more and more important in software development. If the software project does not have a good testing process, as the system grows, both project managers and software developers will lose confidence in the project's prospects, and may even disagree on the project's goals, as there has been no effective control of program code and system design for a long time. Many problems have been temporarily concealed or gradually evolved into other problems. The longer the software development cycle, the more versions of the problem evolve, and the resulting results are "shear, confusion."

Unit tests are the most basic part of the entire test process, and they require programmers to identify problems and give control as early as possible. Also, if there is a problem with the integration test, they can help diagnose it. This lays a solid foundation for establishing an efficient event response mechanism in the software development process.

JUnit provides a framework for implementing unit Tests for Java program developers, making Java unit tests more specification-efficient and more conducive to testing integration.

The internal structure of JUnit

The software architecture of JUnit

JUnit has seven packages, and the core package is junit.framework and Junit.runner. The framework package is responsible for the entire test object's architecture, runner is responsible for testing the driver.

The JUnit class structure

JUnit has four important classes: TestSuite, TestCase, TestResult, Testrunner. The first three classes belong to the framework package, and the latter class is different in different environments. The text test environment is used here, so the Junit.textui.TestRunner is used. The responsibilities of each class are as follows:

1.TestResult, responsible for collecting the results of the testcase, it divides the results into two categories, customer-predictable failure and no predictive error. Also responsible for forwarding test results to Testlistener (the interface is inherited by Testrunner) processing;

2.TestRunner, a starting point for client-object invocation, responsible for tracking the entire test process. can display the test results returned and report the progress of the test.

3.TestSuite, responsible for packing and running all the testcase.

4.TestCase, the class that the customer tests the class to inherit, is responsible for initializing the client class when testing, and testing method invocation.

There are two other important interfaces: Test and Testlistener.

1.Test, contains two methods: Run () and counttestcases (), which is the extraction of the characteristics of the test action.

2.TestListener contains four methods: Adderror (), Addfailure (), Starttest (), and Endtest (), which is the processing of test results and the extraction of the action features of the test drive process.

The following two class diagrams (limited in length, showing only the main part) illustrate the relationships between classes and the design goals of JUnit (Figure 1). The class of the test case uses composite mode. In this way, the customer's test object is turned into a "partial-whole" hierarchy. The client code simply inherits the class TestCase and can easily be combined with other existing objects, making the integration of unit tests more convenient.

Figure 1 Test structure diagram

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.