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.
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.
If you using spring boot, then H2 is one of the popular in memory databases. Spring Boot has very good integration for H2.
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
Related
After a lot of reading in internet, i found out that it seems to be a good practice to use a in-memory DB like H2 to unit test the DAO layer. The idea behind is to avoid working with the production DB.
Fine, so I set up a H2 DB and activated the H2 PostgreSQL Compatibility Mode, since my production DB is on Postgres. The problem I'm now facing: when I run on H2 the original SQL queries in order to build the test DB, this query is not accepted as valid by H2:
ALTER SEQUENCE MYERP.ecriture_comptable_id_seq OWNED BY MYERP.ecriture_comptable.id;
I guess that using the PostgreSQL Compatibility Mode is not a guarantee that all the Postgres specific syntax will be accepted by H2. And probably this would also happen with other DB such as MySQL.
So what's the point in using a h2 database in this case?
Did I miss something?
Unit testing should focus on testing a unit. PostgreSQL is external to your application and thus trivially not part of any DAO that uses it: no 'unit test' should be written that uses any such external database.
Using the database for integration tests may be fine but as you already noticed multiple problems may arise when you use a different database to execute your integration tests (I know very little applications that only use ANSI-SQL without any triggers or other vendor specific SQL extensions like UPSERT in PostgreSQLs case or the SQL in your post).
So what if your production base is in-memory too? That's quite difficult and I would argue an integration test would be fine, just don't call it unit test. All of this just highlights the pain points of doing business logic in your database (e.g. through triggers that you now can't test) or using vendor specific sql which has several advantages that don't play so well with changing the underlying database vendor.
Is it possible to create a CRUD system without database in spring mvc framework?
If possible then which way?
I made an application where i can save and update a single value but i need to save and update a list of value.
HSQLDB offers in memory DB, you can use it to do crud operations for your unit tests. You can maintain spring configuration for the Unit tests and another spring configuration for deployed code. The db configuration can be different for both, so, you run the data updates on the in-memory databse when running tests and actual database when deployed to server. Spring takes care of this seamlessly.
http://hsqldb.org/ spring boot has this inbuild
Other ways to test out your code is to using mockito where you test your code on mock data/objects.
Update: there is something called DBUnit, just found out, it can also help you test against fake db
You Can recreate a CRUD operation using File Systems In Java using a File or Excel as a Database and and then Using File Handling or Apache POI in order to Create Update or Delete.
I've read the following posts:
Is there a way to run MySQL in-memory for JUnit test cases?
Stubbing / mocking a database in .Net
SQL server stub for java
They seem to address unit/component level testing (or have no answers), but I'm doing system testing of an application which has few test hooks. I have a RESTful web service backed by a database with JPA. I'm using NUnit to run tests against the API, but those tests often need complex data setup and teardown. To reduce the cost of doing this within a test via API calls, I would like to create (ideally in memory) databases which can be connected to via a DB provider using a connection string. The idea would be to have a test resource management service which builds databases of specific types, allowing a test to re-point the SUT to a new database with the expected data when it starts - one which can simply be dropped on teardown.
Is there a way, using Oracle or MSSQL, to create a database in memory (could be something as simple as a C# DataSet) which the web server can talk to as if it were a production database? Quick/cheap creation and disposal would be as good as in memory, to be honest.
I feel like this is a question that should have an answer already, but can't find it/ don't understand enough to know that I've found it.
I am trying to find a way to unit test my MySQL dependant code. I know how I would like it to work but cannot find the solution that would work for me. I have looked into DBUnit, but it would seem (if I am not mistaken) that this would require a running database and just aids with the unit testing side of things. I would like some way to avoid running a mysql database when testing. What would work great would be some sort of MySQL spoof driver that actually stored data in memory, rather than needing to access a real persistent database.
In my code it is hard coded to access a MySQL database so I can't just inject some mock object. The way I would like it to work is that when my code calls:
DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
It actually gets some other local database that can either be configured via maven or in the setUp of the maven test. I have looked into memory based databases such as HSQLDB but can't find a way for it to spoof the MySQL driver.
Are there any tools that provide what I am looking for? Do you have any good methods for testing MySQL dependant code?
I have had several projects in which I had to do integrations test against a running MySql server. Instead of spending time setting it up every time, I developed a library that sets up a local running instance of MySQL every time you run your tests.
With that you get a test database that acts like the real thing (because it is) without having to set it up.
DBUnit is also a good alternative if you want to mock the database integration (as far as I know, there is no need for a real MySql server when using DBUnit).
So, I have a Java EE application using Spring framework and JDBCtemplate. And, my application has to do several JDBC database read requests (no/very little writes) on the same database (which is a Postgres DB but is not normalized for a bunch of reasons) but with different sql statements (different where clauses). So, given this situation, I would like to be able to cache the database and be able to run queries on the cache, thereby saving me expensive JDBC calls. So, please suggest appropriate tools or frameworks or any other solutions.
You can start with using simple maps depending the query parameter you are using. A more viable solution is using ehcache.
If you use Spring 3.1 or later, you can use #Cacheable on methods. You need to include <cache:annotation-driven /> in your application context configuration. For simple cases you may use spring's ConcurrentCacheFactoryBean as cache manager. For more complex cases you can use ehcache via spring's ehcache adapter. Use #CacheEvict to reset cache.