简体   繁体   中英

Spring Unit Tests - DB Unit Testing

I am referring to this simple example on how to create a DB Unit Test for an "Insert" DB operation in a Spring application:

Junit test case for database insert method with DAO and web service

I have a similar add() method that I want to unit-test. But in that example, the solution is to do a findById(..) fetch afterwards to verify the insertion.

My question is, my application has no need for this findById operation, so do I need to still add it to my DAO object just for the purpose of unit testing? I would prefer to keep my DAO geared toward the specific actions required by the app only. Also, if I add this findById(..), this new operation itself will require a unit test?

What are the standard solutions to this problem? Do unit tests require their own DAO architecture?

There are two ways to approach this. Testing DAO methods in particular is more about integration testing already since you test the database. If you test your service layer methods, then this would involve stubbing/mocking out the DAO dependencies and just test the service method logic.

Ideal way to do it would be to test both service and DAO layers. The link reference you gave has some very bad examples of unit testing, there's a lot better material out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM