Failed to replace DataSource with an embedded database for tests Spring Data JPA

本文介绍了解决SpringDataJPA测试用例中出现的错误:无法使用内嵌数据库替换数据源的问题。通过添加@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.NONE)注解关闭内嵌数据库,或者将MySQL依赖替换为H2来解决。

跑Spring Data JPA测试用例时,报的错

Error creating bean with name ‘org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration’: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Failed to replace DataSource with an embedded database for tests. If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoConfigureTestDatabase.

其中比较容易定位到问题的是这句:

Failed to replace DataSource with an embedded database for tests.

大致意思是无法用内嵌数据库(如H2)替换数据源。也就是找不到内嵌数据库。我项目本身是用mysql的,没引入H2的依赖,因此这里报错。

但是为什么没有用我本身引入的MySQL呢?
我的猜测是跑JPA单元测试时,自动切换到了内嵌数据库,那应该有地方把它关掉吧? 没错,就是它
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
加上这个注解把它关掉就行
在这里插入图片描述
当然如果想用内嵌数据库也是可以的,把MySQL的依赖去掉,改为H2的就行了
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值