Skip to main content
2 of 3
added 329 characters in body

Terminology, and personal preferences/project guidelines aside, it depends.

Let's take two scenarios:

Function A

This function is expected to do some complex calculations and then do a simple CRUD to a table. The "brains" are in the client code and the database only serves to save state.

A mock, if commonly used in the project, is reasonable enough.

Function B

This function either calls a stored procedure or it launches one or several complex queries in order to compute some results. The "brains" are more in the queries themselves.

If your app is database-centric, it doesn't make as much sense to use a mock because most of the actual real life complexity of the application resides in the database.

Other factors

  • Are the database or queries read-only? That would lessen the need for Mocks

  • How "heavy" is your database? A multi-GB ERP test database with millions of rows in hundreds of tables is not the same thing as using Django tools to quickly setup fake tables and data derived from its ORM-backed schema.

  • Do you/your project practice a lot of Mocking? Unit testing vs Integration testing?

Some well regarded developers are on the record as dismissing Mocks for hiding and/or inducing complexity. While others tend to privilege integration testing, i.e. exercising a number of components to produce an externally observable unit of work over unit testing - exercising a sub-component's internal logic.

Both forms of tests could be using the same testing frameworks - say the JUnit family - and just differ in emphasis. Still unit testing leans more towards mocking, while integration testing leans more towards interaction with actual databases.

  • Even projects that are more focused on integration testing can also find value in granular unit testing: if you have a particularly hairy function you may want to exercise with numerous calls with different arguments and edge cases. Those don't really benefit all that much from repeatedly calling a database to really update it. A Mock would be preferable in that case.

  • That would doubly apply if you planned to stress it some more with automated calls from a fuzzer.