Test cases: shredding a database using Spring beans

Our application has a service layer and a DAO layer written as Spring beans.

When testing the level of service, I don’t want to depend on the real database, so I mock that by creating a “Mock” ​​Impl for the DAO layer

Therefore, when I test the service level, I associate the beans service level with Mock DAO beans A in Production - binds the service layer to the "real" DAO beans

Is that a good idea? Any alternative suggestion on how to mock a database tier?

Clarification: This question is about testing the level of service, not the DAO level. When testing the level of service, I assume that either the DAO layer has already been tested or does not need to be tested. The main thing is how we test the service level, regardless of the DAO implementation, so I mock the DAO level

+5
source share
4 answers

This is a technique that we have been using for many years. Note that when it comes to mocking DAO interfaces, you have a choice:

  • Creating mock instances as real Java classes
  • Use dynamic falsification like jMock (my preference) or EasyMock

( , 1 , , ), , ,

+5

. , - ; , !

+1
+1

, DAO, , , DAO .

, unit test DAO . DAO-, , .

, , ​​ H2, HSQL Derby Java 6 Derby, , , , . Spring 3 " ". RC1 jdbc, . . .

But even with the current Spring 2.5 branch using the embedded database, it is just a matter of creating JAR databases and setting it up accordingly DataSource.

0
source

All Articles