简体   繁体   English

Spring启动数据JPA任务:测试失败

[英]Spring boot Data JPA task :test Failed

I'm starting to use Data JPA from Spring to interact with Postgres Database, but when I try to build the project, the building phase raise an exception in the applicationTest.我开始使用 Spring 中的 Data JPA 与 Postgres 数据库交互,但是当我尝试构建项目时,构建阶段在 applicationTest 中引发异常。

This is my build.gradle :这是我的build.gradle

plugins {
    id 'org.springframework.boot' version '2.5.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'eu.universome.radio'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    
    implementation 'org.springframework.boot:spring-boot-starter-web'
    //implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

If I comment the dependencies: implementation 'org.springframework.boot:spring-boot-starter-data-jpa' the build will be successful.如果我评论依赖项: implementation 'org.springframework.boot:spring-boot-starter-data-jpa'构建将成功。

This is my empty applicationTest :这是我的空applicationTest

package eu.universome.radio.RadioUVMServer;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class RadioUvmServerApplicationTests {

    @Test
    void contextLoads() {
    }

}

Here there is the Console Error:这里有控制台错误:

Gradle Distribution: Gradle wrapper from target build
Gradle Version: 7.1.1
Java Home: /usr/lib/jvm/java-11-openjdk-amd64
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: build

> Task :compileJava
> Task :processResources UP-TO-DATE
> Task :classes
> Task :bootJarMainClassName
> Task :bootJar
> Task :jar UP-TO-DATE
> Task :assemble
> Task :compileTestJava
> Task :processTestResources UP-TO-DATE
> Task :testClasses

> Task :test FAILED

RadioUvmServerApplicationTests > contextLoads() FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
        Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at ConstructorResolver.java:800
            Caused by: org.springframework.beans.factory.BeanCreationException at ConstructorResolver.java:658
                Caused by: org.springframework.beans.BeanInstantiationException at SimpleInstantiationStrategy.java:185
                    Caused by: java.lang.IllegalStateException at Assert.java:97

1 test completed, 1 failed

Here you can find the report: [1]: https://www.universome.eu/wp-content/uploads/RadioUVM/progetto-radio-unime/reports/tests/test/classes/eu.universome.radio.RadioUVMServer.RadioUvmServerApplicationTests.html在这里您可以找到报告:[1]: https : //www.universome.eu/wp-content/uploads/RadioUVM/progetto-radio-unime/reports/tests/test/classes/eu.universome.radio.RadioUVMServer .RadioUvmServerApplicationTests.html

  1. Not sure if you have the dependency added.不确定您是否添加了依赖项。

implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.23'

  1. You can check if the driver is on classpath:您可以检查驱动程序是否在类路径上:
try {
      Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
      // recheck dependencies
    }

Solved adding the line解决了添加行

runtimeOnly 'org.postgresql:postgresql'

on build.gradle file in section dependencies在部分依赖项中的build.gradle文件上

dependencies {
    
    implementation 'org.springframework.boot:spring-boot-starter-web'
    //implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'org.postgresql:postgresql'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM