spring启动数据jpa任务:测试失败

7hiiyaii  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(322)

我开始使用spring中的数据jpa与postgres数据库交互,但是当我尝试构建项目时,构建阶段在applicationtest中引发了一个异常。
这是我的身材。格雷德尔:

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()
}

如果我对依赖项进行注解: implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 构建将会成功。
这是我的空应用程序测试:

package eu.universome.radio.RadioUVMServer;

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

@SpringBootTest
class RadioUvmServerApplicationTests {

    @Test
    void contextLoads() {
    }

}

这里有一个控制台错误:

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

您可以在这里找到报告:[1]:https://www.universome.eu/wp-content/uploads/radiouvm/progetto-radio-unime/reports/tests/test/classes/eu.universome.radio.radiouvmserver.radiouvmserverapplicationtests.html

dxxyhpgq

dxxyhpgq1#

不确定是否添加了依赖项。 implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.23' 您可以检查驱动程序是否在类路径上:

try {
      Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
      // recheck dependencies
    }
nfzehxib

nfzehxib2#

解决了添加行的问题 runtimeOnly 'org.postgresql:postgresql' 在节依赖项中的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'
}

相关问题