1

So this is the first time I am trying to write unit test for my sping-boot-application in java(it seems surprising but yes :D).

I was trying to use h2 db for my unit test, But I figured out there are some restrictions of this in memory db. When I try to run my unit tests. It's showing me error like.

2018-10-31 15:32:21.796 [] [] [Test worker] ERROR org.hibernate.tool.hbm2ddl.SchemaExport :485 - Unknown data type: "JSON"; SQL statement:

I instantly jumped to google how can it possible, then I discovered that h2 db does not support JSON fields. Although there are some workarounds to skip the JSON data but as I am using @Type So, I can't using workarounds like changing to Text or something.

I am curious is there any other method I can use to complete my unit testing using some other in memory db.

Geek_To_Learn
  • 1,816
  • 5
  • 28
  • 48
  • What´s your production RDBMS? Simply put: you can´t replace one database with another and expect everything to work. There are plenty of differences other than JSON fields that you may encounter. – Serg M Ten Oct 31 '18 at 11:08
  • production DBMS is mysql – Geek_To_Learn Oct 31 '18 at 12:00
  • @SergMTen depends on the scope of the test suite. As long as you don't use engine specific stuff, H2 is a fine substitute for unit tests that require some form of data storage, especially for JPA and Hibernate code. – jwenting Oct 31 '18 at 12:21
  • Well there are already issues with the JSON fileds, so who knows how many more features specific of MySQL are in use? If feasible, I´d advise that for test it might be easier to switch MySQL to INMEMORY engine using `CREATE TABLE tablename (...) ENGINE = MEMORY` else MariaDBj may be another option. Last if still going for H2, add MODE=MySQL to the data source bean `` – Serg M Ten Oct 31 '18 at 13:01
  • Not sure this is the perfect solution, but I have found a walthrough to use JSON data types in H2. I have added INIT=create domain if not exists json as other line and it's working for now. – Geek_To_Learn Oct 31 '18 at 14:07
  • Why don't you use docker based integration test? [Check here](https://stackoverflow.com/a/66259759/410439) – Ravi Parekh Sep 15 '21 at 13:34

0 Answers0