Order
Unit testing is a very useful tool that is part of a Java programmer's upbringing. Starting in 04, I started using unit tests in project practice. For more than more than 10 years, participating projects have mostly used unit tests, more than 100% coverage requirements, and less to cover the main code, and found that this practice is good for improving software quality and development speed. In the communication with friends and colleagues, found that there are many projects do not use unit testing, there are many programmers do not write unit tests, but everyone is interested in this. So I want to share my skills (architecture, design and technology) and implementation strategies that I've mastered about unit testing. Of course, I would be happy if there was a company willing to ask me to do the relevant internal training and consultation.
Unit testing the series wants to talk about two things:
1. How to use unit tests in a project
2. Techniques for Java Unit Testing
3. Some mistakes in implementing unit testing
This article includes the following sections:
1. Definition and classification of unit tests that I understand
2. Benefits of implementing Unit tests
3. Java Programmer Unit Testing Tool Library
Definition and classification of unit tests
The definition of unit tests refers to the code that programmers write to test the logic code. In my understanding, unit tests are divided into the following two categories:
Code testing
Code testing refers to the implementation of the code to test, independent of the third-party environment, running fast. For a few examples:
- The Doget method of testing the Acontroller object correctly passes the parameters P1 and P2 to the query method of the Bservice object.
- Test the Get method of a DAO class to
"select a,b from t where l=5"
pass jdbcTemplate
the method correctly query
and return the result of the query.
Integration Testing
Integration testing refers to testing that integrates with third-party dependencies. It relies on third-party environments, which run at slower speeds than code tests. For a few examples:
- Testing the rest service with the HTTP protocol
- Call the DAO class and test it to get the data correctly from the database
Implement the benefits of unit testing for early testing
In software engineering, we all know that the sooner you start testing the better, the sooner you start testing the sooner you can find the defect, the less the cost of the modification.
But testing for software systems requires you to write at least one running minimized system before you can do it. Before the appearance of an operable interface seen by outsiders, it has been a sweat for developers to work hard for several days. But in accordance with the traditional methods of work, all these efforts, before the interface is not out, in addition to know that they can be compiled through, unable to conduct any test. What's more, if your system relies on a non-implemented third-party system, your code will not have any valid tests until that third-party system responds correctly to your request.
But using the unit test technique, using stub and mock techniques, we can test the code without relying on the interface, without relying on a third-party system, or even, you can implement a method to test a method without having to wait for the entire function to complete the re-test.
You can safely modify the code and refactor the code
Unit testing can be automated, if the code has good unit test coverage, after the code is refactored and modified, we can use a very short time (a few minutes) to test the entire system, test all pass, The changes to this description do not have a serious impact on the system (this does not explain where the unit tests are not covered), so you can continue to modify the code and refactor the code with peace of mind.
Helps improve code quality and architecture design
To facilitate unit testing and code coverage, the architecture will inevitably be optimized in terms of logic encapsulation, decoupling, dependency injection, control inversion, external interface encapsulation, and so on.
For a few examples:
- Both the logic code and the interface code are written together, causing the test of the logical code to include parsing of the interface elements. Thus, "separation of logical code and Interface Code" becomes the least expensive solution for stopping unit tests
- Instantiating a dependent object in your code can make it harder to isolate the test and cannot be tested until the object being relied upon is implemented. As a result, dependency injection is the best choice.
Repeatable Code Validation
Well-written unit test code can be run automatically, can be run in all developer environments, can be run in a continuous integration environment, can be run after each code modification commit, the code and function of the project are repeatedly received test.
Speed up development
A small upfront investment that can work throughout the project cycle improves the quality of the code, enables early testing of the code, reduces code bugs, and ensures that the code is not broken by other code. The time to modify bugs and debugs, and the associated communication time, has been drastically reduced. Project time can be more assured.
Play the role of a code document
For the use of a method or an object, it is sometimes difficult to make a clear comment. The use of the method/object in the unit test provides an example of how it is used, making it easier for us to understand its use.
Java Programmer Unit Testing Tool Library
Writing unit tests also requires some tools, but these tools are relatively simple to use and easy to learn. In a later article, I'll cover a few of them that I often use.
Unit Test Framework
In the Java world, there are two most used unit test frameworks, JUnit and testng. The most widely used and supported is junit. In the following article I will give you a brief introduction of its use.
Mock tool
The mock tool is a tool that must be mastered for code testing. There are three kinds of mockito, Easymock and jmock that are popular now. I used Easymock and Jmock before, and then I started using Mockito and I couldn't stop. The following article will give you a brief introduction of its usage.
Other tools
General-purpose I will introduce hamcrest, a tool library for validating test results. There are a number of other tools used for a variety of specialized purposes, such as
- Spring test, a necessity for integration testing of spring programs, I'll cover in a later article
- The spring MVC Test Framework, a tool for Web apps written by spring MVC, I'll cover in a later article
- DBUnit, this also has many people to use, but I basically did not use, therefore did not introduce. If you are interested, you can go and see for yourself.
- rest-assured, Java DSL for easy testing of REST services, it looks beautiful, has not been used, and so I learn to use the end feel good to share again.
A unit test of the Java Programmer's path to cultivation