跑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)
加上这个注解把它关掉就行
Failed to replace DataSource with an embedded database for tests Spring Data JPA
当然如果想用内嵌数据库也是可以的,把MySQL的依赖去掉,改为H2的就行了
Failed to replace DataSource with an embedded database for tests Spring Data JPA

相关文章:

  • 2021-10-03
  • 2022-01-10
  • 2021-07-23
  • 2021-06-16
猜你喜欢
  • 2021-11-19
  • 2021-09-05
  • 2021-08-03
  • 2021-10-19
相关资源
相似解决方案