在Junit5中使用H2作为测试DB

发布于 2025-01-25 20:30:00 字数 1328 浏览 3 评论 0原文

我正在春季启动中开发一个应用程序。我使用PosttreesQL DB进行生产,我希望我的Junit 5测试能够在H2内存DB上进行。问题是,经过一些配置后,测试似乎仍然无法在内存db上运行:

  • i可以从prod db访问实体(一个保存的实体在prod db tho中不持续)

  • 如果我在测试日志中抓取了hibernate usg.hibernate.dialect.postgresqlesclesclesect

@DataJpaTest
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.properties")
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
public class ClinicTest {
    @Resource
    private ClinicRepository clinicRepository;

    @Test
    public void givenClinic_whenSave_thenGetOk() {

        var clinic = new Clinic();
        clinic.setName("asd asd");
        clinic.setShortName("asd");
        
        // the ids of prod db entities are printed
        clinicRepository.findAll().forEach(clinic1 -> System.out.println(clinic1.getId()));
        

        // the id keeps incrementing between runs
        System.out.println(clinicRepository.save(clinic).getId());
    }

}

application-test.properties

jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:testdb
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create-drop
spring.test.database.replace=none

我应该更改以在存储数据库中的H2上运行独立测试吗?

I'm developing an app in Spring Boot. I use a PostreSQL DB for production, and I want my JUnit 5 tests to run on a H2 memory DB. Problem is, after some configuration, the tests still don't seem to run on the in-memory db:

  • I can access entities from the prod db (a saved entity doesn't persist in the prod db tho)

  • if I grep through the test logs, I can see that Hibernate uses org.hibernate.dialect.PostgreSQLDialect

@DataJpaTest
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.properties")
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
public class ClinicTest {
    @Resource
    private ClinicRepository clinicRepository;

    @Test
    public void givenClinic_whenSave_thenGetOk() {

        var clinic = new Clinic();
        clinic.setName("asd asd");
        clinic.setShortName("asd");
        
        // the ids of prod db entities are printed
        clinicRepository.findAll().forEach(clinic1 -> System.out.println(clinic1.getId()));
        

        // the id keeps incrementing between runs
        System.out.println(clinicRepository.save(clinic).getId());
    }

}

application-test.properties

jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:testdb
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create-drop
spring.test.database.replace=none

What should I change to run self-contained test on a H2 in memory database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆悲凉 2025-02-01 20:30:01

默认情况下,Spring为测试创建了H2 DB。您无需在属性文件中指定它。

只需在pom.xml中添加H2并将其范围设置为测试即可。春季靴子应处理其余的。

Spring, by default, creates an H2 DB for the test. You don't need to specify it in your properties file.

Just add h2 in your pom.xml and set its scope to test. Spring Boot should handle the rest.

你是暖光i 2025-02-01 20:30:01

您的application-test.propties没有用户和pasword
尝试添加此信息:

#--------------------- DB Connection ------------------
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=

#--------------------JPA-ORM Properties-----------------
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.format_sql=true

Your application-test.proprties does not have user and pasword
try adding this:

#--------------------- DB Connection ------------------
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=

#--------------------JPA-ORM Properties-----------------
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.format_sql=true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文