Diagnosing Java code: Making a logger test to call the method correctly

Source: Internet
Author: User

Unit testing with JUnit is a powerful way to ensure the integrity of your code base, but some invariants are more difficult to test than others (which is one of the method call sequences). In the part of diagnosing Java code, Eric Allen describes how to use a logger (a special listener) in your unit tests to ensure that a method call sequence occurs in the proper order. Please click on the discussion at the top and bottom of the article to share your views on this article with the author and other readers in the forum.

Over time, the JUnit framework provides a good way to improve the robustness of systems when system developers, maintainers, and even systems specify changes. By testing, you can check that certain invariants of your code are supported.

Tests are usually divided into two categories: unit and Acceptance Test:

Unit tests ensure that components are composed to perform the functions they should complete.

Acceptance testing ensures that the system's highest-level functionality appears in front of the user, consistent with its design-time functionality.

JUnit can help with unit testing.

Ideally, unit tests developed for the system would completely overwrite the settings of the component's expected invariants and ensure that any changes made by the new developer would not break the existing code.

In fact, some invariants will be ignored by the test. Part of the reason is that some invariants fall into the interaction of many isolated components of the system when they do not reach a comprehensive system test level.

In this article, I'll discuss one of those types of invariants and how to use a complex unit test to check the invariants. The invariant type I'm going to discuss is the appropriate order for a set of related method sequence calls.

Shake hands with JUnit

Before continuing, it is important to familiarize yourself with JUnit and learn how to use it easily to write unit tests for your code. In the Resources section, I've included a link that links to all the information you need to download and start using JUnit. (If you are familiar with JUnit, skip directly to the 1th example.) )

Unit tests provide developers with the following features:

Design a class from an interface perspective

Remove class clutter from the release package

Automatically identify changes to catch errors

The unit test process is typically performed in the following steps:

Decide what your component should do.

Design your component formally (or informally, depending on complexity).

Write out unit tests to check the activity of the component. (In this step, the test will not compile; The code hasn't been written yet.) The purpose of writing tests is to help determine the functional purpose of a component. )

Write the component code as designed, and if necessary, reorganize the unit.

Stop the encoding process when the test is passed (starting from step 3rd).

Brainstorm the possibilities for other code interrupts, write out tests to confirm, and then modify the code.

Each time a flaw is detected, a new test will be written.

Every time you change the code, start all the tests again.

JUnit is a simple framework created by Erich Gamma and Kent Beck that can be used to write repeatable tests that make it relatively easy to build a test suite that can add changes, which helps developers assess the progress of development and detect unintentional effects. JUnit is an example of a xUnit architecture.

With JUnit, each test instance inherits the TestCase class. Each of the public methods without parameters, whose name begins with "Test", is executed one at a time. The test method invokes the component under test and asserts the behavior of the component. JUnit also reports the location of the failure when assertions cannot be made.

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.