跑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的就行了

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

被折叠的 条评论
为什么被折叠?



