When using a database in unit testing, you can consider two options:
Build a long-term test database as a unit test. Clear irrelevant data before starting or after the test is completed to ensure the repeatability of the test. The disadvantage is that unit tests may fail when multiple people run them at the same time.
Use an in-memory database (such as H2). The advantage is that there is no need to clear irrelevant data. The disadvantage is that the database initialization process (such as table creation statements) must be included in the unit test. If the initialization is complex, it will also affect the efficiency of unit testing.
As for how to verify the query results, it is basically based on business logic. For example, when my unit test runs here, the query will definitely return 27 records, so verify whether the number of returned records is 27. In other cases, you can design it yourself.
When using a database in unit testing, you can consider two options:
Build a long-term test database as a unit test. Clear irrelevant data before starting or after the test is completed to ensure the repeatability of the test. The disadvantage is that unit tests may fail when multiple people run them at the same time.
Use an in-memory database (such as H2). The advantage is that there is no need to clear irrelevant data. The disadvantage is that the database initialization process (such as table creation statements) must be included in the unit test. If the initialization is complex, it will also affect the efficiency of unit testing.
As for how to verify the query results, it is basically based on business logic. For example, when my unit test runs here, the query will definitely return 27 records, so verify whether the number of returned records is 27. In other cases, you can design it yourself.