【问题标题】:How to use server database in TestContext in spring instead of the in memory h2 database?如何在 Spring 中使用 TestContext 中的服务器数据库而不是内存中的 h2 数据库?
【发布时间】:2016-11-17 01:06:28
【问题描述】:

我需要在我的TestConfiguration 中使用服务器数据库。我在 application.properties 中定义了一个 Postgresql 数据库,应用程序应该使用它,但是 TestContext 使用的是嵌入式 h2 数据库。 application.properties 有以下内容:

H2 被定义为 pom.xml file 中的一个依赖项,当我删除并尝试构建时不会出现错误:

Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:180)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)

我做错了什么?

【问题讨论】:

  • 我在问题中添加了错误。
  • 类路径中有 postgre 驱动程序吗?
  • postgresql 是在 pom 中定义的。
  • 您必须告诉您的TestContext 使用在您的application.properties 上定义的数据库详细信息。我猜它在构建时找不到属性文件。

标签: java spring maven spring-boot spring-data-jpa


【解决方案1】:

问题是我在 pom 中为 postgresql 定义的范围。

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

当我将范围设置为提供一切正常时。

【讨论】:

    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 2019-03-03
    • 2019-09-13
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多