How to perform unit tests with h2 database where bpchar is used?

送分小仙女□ 提交于 2021-01-29 05:28:40

问题


I have a spring + hibernate application that uses postgres database. I need to write unit tests for the controllers. For tests I wanted to use h2 database but unfortunately test crashes during create-drop leaving me with information that bpchar data type is invalid. I wonder how to solve this issue so I could run tests.

I can't change my columns with bpchar to varchar, it need to stay as it is. I also tried to set postgresql mode but it didn't helped. Am I right that the only solution I have is to use embedded postgres database in order to perform tests or is there any other approach that I could use?


回答1:


Am I right that the only solution I have is to use embedded postgres database in order to perform tests or is there any other approach that I could use?

You try to use postgres-specific data type with h2 (which does not have it). Of course, it does not work.

If you cannot change type of this field - use embedded postgres in tests.




回答2:


One interesting approach to this issue is Test Containers

Since Postgres doesn't have an embedded mode, but you can use the aforementioned framework to start the docker container before the test, create a schema and apply migrations if you're using something like Flyway or Liquibase or integrate your custom solution.

The idea is that the container will be prepared and available to the test when it runs. After the test passes (regardless of the actual result, success or failure) you can stop the container.

Firing up the container can be quite expensive (a matter of seconds), however you can take advantage of spring caching configurations during the tests, so when the first test in the module starts, the container is actually started, however, it gets reused between the tests and the test cases, since the application context doesn't get re-started.

Keeping the database clean between tests also becomes a trivial task due to Spring's @Transactional annotation that you put on a test case, so that spring artificially rolls back the transaction after each test. Since in Postgres, even DDL commands can be transactional it should be good enough.

The only limitation of this approach is that you should have a docker available on build machine or local development machine if you're planning to run these tests locally (on Linux and Mac OS its not a problem anyway, but on Windows you need to have Windows 10 Professional edition at least in order to be able to install docker environment).

I've used this approach in real projects and found it very effective for integration testing.




回答3:


Actually you can do this in your application.properties to let H2 know:

spring.datasource.url=jdbc:h2:mem:testdb;INIT=CREATE TYPE BPCHAR AS CHARACTER NOT NULL

Also make sure auto configuration of the database is turned off for your test. You can do this by adding:

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class MyTestForTableWithBpcharColumn {


来源:https://stackoverflow.com/questions/56434081/how-to-perform-unit-tests-with-h2-database-where-bpchar-is-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!