user3394305
user3394305

Reputation: 3

Mock Database, MockMvc

I have a simple REST app with MySQL database, everything works fine, but while testing do we need to create a dummy object and test on it, or test via Mock database? The dummy object has quite large constructor and nested classes, which is a lot of work.

Upvotes: 0

Views: 1620

Answers (3)

cassiomolin
cassiomolin

Reputation: 130857

For integration tests, you should consider using a database in-memory, such as H2.

H2 supports compatibility modes for IBM DB2, Apache Derby, HSQLDB, Microsoft SQL Server, MySQL, Oracle and PostgreSQL. To use the MySQL mode, use the database URL as shown below (and refer to the documentation for further details):

jdbc:h2:~/test;MODE=MySQL;DATABASE_TO_LOWER=TRUE 

Upvotes: 0

Matt Vickery
Matt Vickery

Reputation: 121

IMO, there's little point using a mock database, unless you're testing connectivity handling. For example, how does my application behave if the database connection is dropped etc.

For testing SQL, you will do no better than testing against the actual database you're going to use in production. If you use another database as a substitute, i.e. H2, make sure you understand that you are testing a DB driver and database that will be different to your production deployment and this means that you may not catch potential errors in your tests that use this setup.

For testing data handling, you could also use a mock of some kind but again, if you're always going to be better off using the actual database you will be using in production, whenever you can.

If you're using Hibernate as an ORM provider, as part of setting up your integration tests, you can have it execute DML scripts to load your data for testing purposes.

Upvotes: 1

wolfdale
wolfdale

Reputation: 1

If you using spring boot, then H2 is one of the popular in memory databases. Spring Boot has very good integration for H2.

Upvotes: 0

Related Questions