How to make unit testing of data source layers

xiaoxiao2021-03-06  16

In a typical three-layer architecture, there is a separate data source layer. Mainly used to carry out the persistence of data. Of course, most of them are data stored in the relational database. We generally use the DAO mode to block the data, take the details, which is just exposed to calling its business logic layer, there are many ways to implement the implementation of the specific interface, such as JDBC, Hibernate, JDO, JDBC, Ibatis, JDO , Entity Beans, etc. In the following discussion, it is assumed that it is now very popular. In general, there are two ways: using the MOCK object and test directly in the relational database. First, use the MOCK object to test DAO. It shields specific relational databases, which has the advantage of being able to write convenience in test code and can run quickly. Disadvantages: The risk is too small to test the data layer, shields a lot of problems with databases, such as: object and database tables mapping, query statements are correct. Second, test directly in the relational database. Advantages: Full tests can be performed on the data layer. Disadvantages: The unit test runs too slow, and the database is frequently operated. I personally test the data layer in the actual project. Although this unit test is slower, it can be thoroughly testing the data source layer in the early days, reducing the risk of the project. Data source layer test content: 1. Entity relationship test: association (one-to-one, multi-one, one-to-many, multi-to-many), inheritance (inheritance of class, inheritance). It is mainly to test the mapping between entity objects and tables (tables in the database). 2, query the test of the statement. First, it is definitely to ensure the correctness of the query statement syntax, I think the correctness of the query results should also be guaranteed by unit testing (some views that the verification of query results is the category of integration tests). 3. The simple GET () and set () method do not have to waste time test, very simple CRUD operation is not tested, but if there is a logical part, it is necessary to test.

转载请注明原文地址:https://www.9cbs.com/read-41157.html

New Post(0)