栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

嵌入式Postgres用于Spring Boot测试

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

嵌入式Postgres用于Spring Boot测试

我是@MartinVolejnik提到的嵌入式数据库弹簧测试库的作者。我认为该库应该可以满足您的所有需求(PostgreSQL + Spring Boot + Flyway
+集成测试)。非常抱歉您遇到了麻烦,所以我创建了一个简单的演示应用程序,演示了该库与Spring Boot框架的结合使用。下面,我总结了您需要执行的基本步骤。

Maven配置

添加以下Maven依赖项:

<dependency>    <groupId>io.zonky.test</groupId>    <artifactId>embedded-database-spring-test</artifactId>    <version>1.5.2</version>    <scope>test</scope></dependency>

飞路配置

将以下属性添加到您的应用程序配置中:

# Sets the schemas managed by Flyway -> change the xxx value to the name of your schema# flyway.schemas=xxx // for spring boot 1.x.xspring.flyway.schemas=xxx // for spring boot 2.x.x

此外,请确保您不使用

org.flywaydb.test.junit.FlywayTestExecutionListener
。因为该库具有自己的测试执行监听器,可以优化数据库初始化,并且如果
FlywayTestExecutionListener
应用,则此优化无效。

Spring Boot 2配置

从Spring Boot 2开始,Hibernate和Postgres驱动程序存在兼容性问题。因此,您可能需要在应用程序配置中添加以下属性来解决此问题:

# Workaround for a compatibility issue of Spring Boot 2 with Hibernate and Postgres Driver# See https://github.com/spring-projects/spring-boot/issues/12007spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

一个测试类的示例,演示了嵌入式数据库的使用:

@RunWith(SpringRunner.class)@DataJpaTest@AutoConfigureEmbeddedDatabasepublic class SpringDataJpaAnnotationTest {    @Autowired    private PersonRepository personRepository;    @Test    public void testEmbeddedDatabase() {        Optional<Person> personOptional = personRepository.findById(1L);        assertThat(personOptional).hasValueSatisfying(person -> { assertThat(person.getId()).isNotNull(); assertThat(person.getFirstName()).isEqualTo("Dave"); assertThat(person.getLastName()).isEqualTo("Syer");        });    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/435427.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号