Talk about Java unit testing

This article was first published on the personal WeChat public account "andyqian", looking forward to your attention!

foreword

I haven't updated the article for a few days. Tell a lie, I've been busy lately! to be frank. Lazy lately! Some time ago, I read the "Alibaba Java Development Manual" published by Alibaba and thought it was very good. . Some are simply too empathetic. For example: unit testing. I recommend everyone to see it. Well, let's talk about unit testing today.

What is unit testing?

When it comes to unit testing, many developers are easy to ignore. It's even considered a test thing. It needs to be corrected here. For unit testing, Wikipedia describes it like this:

Unit testing, also known as module testing, is a test work for correctness verification of program modules (the smallest unit in software design).

did you see? Unit testing is the work of testing for correctness. Then as a developer who solves the error. But you can't throw mistakes to test classmates, this is obviously digging a hole.

Where do you need to write unit tests?

In day-to-day development, don't think that writing unit tests brings extra work. It seems to reduce the development efficiency. Actually, this is very important. It can also bring benefits to later maintenance. Most bugs can also be detected by unit testing.
Here is where unit tests need to be written:

  1. In the Dao layer, each method in the Service layer ensures the passing of the unit test.
    (We usually use Mybatis as the ORM framework, and it is recommended that SQL can also be executed through the database. This is also a test method.)
  2. In important algorithms, the core business logic must pass unit tests.

In unit testing: we should try to keep the test method atomic. That said, the smaller the unit test granularity, the better.

Why write unit tests?

If you insist on the benefits of writing unit tests? That's just too much. The most direct benefit is to reduce bugs.

  1. Improve code quality.
  2. Improve code readability.
  3. A kind of responsibility for your own code.
    ….

In good open source projects, such as Spring source. We will find that there are a lot of unit tests. Basically every method has unit tests. When we read incomprehensible code. Run a unit test to debug it. Can also be understood quickly!

How to write unit tests?

In Java, unit test code should be written in the src/test/java directory, and unit test resources should be placed in src/test/resources. The most commonly used unit testing framework is Junit. The latest Junit version has been updated to JUnit 5. But it requires JDK version 1.8 and above to run. If needed to support running in JDK version 1.7. We can choose a lower version of Junit.

The following is the official website's user guide address:

https://junit.org/junit5/docs/current/user-guide/

Due to space reasons. This is not a case. Next time I will write a separate article on how to write unit tests.

A little tip for unit testing


In the "Alibaba Java Development Manual", we have a lot of practical advice on unit testing. Here are a few excerpts, children's shoes who are interested. Download to read more:

  1. Keep unit tests independent. To ensure that unit tests are stable, reliable and easy to maintain, unit test cases must not call each other, nor depend on the order of execution.
  2. Unit tests can be executed repeatedly and cannot be affected by the external environment.
  3. The basic goal of unit testing: statement coverage reaches 70%; statement coverage and branch coverage of core modules
    should reach 100%.
  4. During the design review phase, developers need to work with testers to determine the scope of unit tests, which preferably cover all test cases (UC).

…..

There are many companies, not just design reviews. There is also the test review, which is mentioned in item 4 above. Developers and testers work together to evaluate test coverage, as well as test cases.

finally

I'm going home today, and I'm even a little excited. When do you all go home for vacation?

PS: Reply "Alibaba Java Development Manual" in the official account. A commemorative electronic copy is available.

 

write picture description here

 Scan the code to follow and make progress together

Personal blog:  http://www.andyqian.com

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324979909&siteId=291194637