English 中文(简体)
是否有办法在不使用H2 daatabase的情况下为弹簧靴储存库编写测试案例?
原标题:is there any way to write test cases for repositories in spring boot without using H2 daatabase?
I am working on a Spring Boot application and need to write test cases for my repository layer in a way like database should create using my flyway script. However, I want to avoid using an in-memory database like H2 or connecting to an actual database for these tests(if possible). Is there a way to the database interactions in Spring Boot so that I can test my repository methods? If so, could you please provide some guidance or examples on how to achieve this? Here is a brief overview of my setup: Spring Boot version: 3.0.5 Repository interface: Extends JpaRepository postgresql: 42.6.0 flyway: 9.16.3 Thank you in advance for your help! I have tried in H2 using flayway script but it is not working, and giving syntax error beacuse my script contains script for postgresql. and i want to do it using only flayway script.
问题回答
The first thing to ask is always: What do you really want to test. And the repositories isn t really a proper answer to that. What you really want to test is probably, something like: If I call MyRepository.x with parameters y, z I want to get this result back, and not the rows with green mubblewumps. If the repository behaves like this depends on the correct implementation of Spring Data JPA, which depends on the correctness of your JPA implementation. and it depends on the actual database. Therefore there is no way around it: you need a real database, and one that is as close to your production database as possible. And you already noticed that H2 doesn t cut it because it doesn t understand your scripts that (I assume) work fine on an actual Postgres database. Therefore: Use a real Postgres Database managed by Testcontainers




相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签