spring-data-jpa 为什么在运行测试时会出现错误,但在本地我可以很好地使用数据库?

kmynzznz  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(120)

UPDATE: I realized that the problem was related to using the environment variables. When I didn't use them, it worked fine. The situation is exactly the same as this post . I don't know if it is the best solution, but for now I have disabled the test, using @Disabled annotation.

Hi, I have a Spring Boot application. When I run it locally, I can use the DB just fine. I am able to create entities using my api, they are correctly created on the DB.
But when I run the tests for the app, the following error appears:

org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

This is the application.properties:

spring.datasource.url=jdbc:mysql://localhost:${MYSQL_DB_PORT}/myproject-db
spring.datasource.username=${MYSQL_DB_USERNAME}
spring.datasource.password=${MYSQL_DB_PASSWORD}

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

server.servlet.context-path=/myproject-api/v1

(I have the environment variables set on IntelliJ Run Configurations)
These are the dependencies in my pom.xml:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

I know there are a lot of already answered questions about this, but nothing has worked for me so far.

rjee0c15

rjee0c151#

如果我处在你的位置,我会这样做:
1.设置maven surefire以从~/.m2/settings.xml中的配置文件中获取系统属性,如:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>${surefire.version}</version>
 <configuration>
  <systemPropertyVariables>
   <datasource.url>${db.url}</datasource.url>
   <datasource.username>${db.username}</datasource.username>
   <datasource.password>${db.password}</datasource.password>
  </systemPropertyVariables>
 </configuration>
</plugin>

1.使用占位符的真实的值在~/.m2/settings.xml中设置maven配置文件:

<profiles>
 <profile>
  <id>localenv</id>
  <properties>
    <db.url>jdbc:mysql://localhost:3306/myproject-db</db.url>
    <db.username>username</db.username>
    <db.password>password</db.password>
  </properties>
 </profile>
</profiles>

1.将www.example.com放入application.propertiessrc/test/resources中,内容如下:

spring.datasource.url=${datasource.url}
spring.datasource.username=${datasource.username}
spring.datasource.password=${datasource.password}

之后,测试应该可以工作,因为IntelliJ识别这样的配置
此外,您可以通过IntelliJ设置运行主要spring引导类,而无需配置环境变量,但可以利用Maven配置文件。
1.安装SpringBoot Patcher插件
1.将application.properties放置在与src文件夹相同的级别,其内容如下:

spring.datasource.url=${datasource.url}
spring.datasource.username=${datasource.username}
spring.datasource.password=${datasource.password}

1.设置spring-boot-maven插件,例如:

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
 <execution>
  <id>run-app</id>
  <goals>
   <goal>run</goal>
  </goals>
  <phase>none</phase>
  <configuration>
   <mainClass>com.tld.MyCoolaMainClass</mainClass>
   <workingDirectory>${project.basedir}</workingDirectory>
   <systemPropertyVariables>
    <spring.datasource.url>${db.url}</spring.datasource.url>
    <spring.datasource.username>${db.username}</spring.datasource.username>
    <spring.datasource.password>${db.password}</spring.datasource.password>
   </systemPropertyVariables>
  </configuration>
 </execution>
</executions>

相关问题