【问题标题】:import failed using gradle dependency on windows 10 using IntelliJ使用 IntelliJ 在 Windows 10 上使用 gradle 依赖项导入失败
【发布时间】:2020-09-11 11:22:23
【问题描述】:

我正在关注 Spring Boot 文档https://spring.io/guides/gs/spring-boot/ 但是在我做了与添加单元测试步骤相同的操作之后,即使我有相同的 gradle 文件,项目也不会因为导入失败而构建。

gs-spring-boot\complete\src\main\java\com\example\springboot\HelloControllerTest.java:3: error: package org.hamcrest does not exist
import static org.hamcrest.Matchers.equalTo;
                      ^
error: package org.assertj.core.api does not exist
import static org.assertj.core.api.Assertions.*;

我的gradle文件内容

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

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

整个项目设置都在https://github.com/spring-guides/gs-spring-boot/archive/master.zip中给出

这里出了什么问题?我有 java SDK 设置,我可以在这一步之前运行 http 服务器。我在使用 IntelliJ 及其内置 gradle 的 Windows 10 上。

【问题讨论】:

    标签: java spring-boot gradle intellij-idea


    【解决方案1】:

    不确定这是否仍然是一个有用的答案,但我也遇到了这个问题。

    问题似乎是您的代码位于“主”目录而不是“测试”目录中。将测试文件移动到测试目录为我解决了这个问题。

    【讨论】:

      【解决方案2】:

      你错过了Hamcrest

      // https://mvnrepository.com/artifact/org.hamcrest/hamcrest
      testImplementation "org.hamcrest:hamcrest:2.2"
      

      【讨论】:

        猜你喜欢
        • 2020-10-23
        • 1970-01-01
        • 2020-02-05
        • 2016-10-08
        • 2015-02-25
        • 2012-10-25
        • 1970-01-01
        • 1970-01-01
        • 2021-11-26
        相关资源
        最近更新 更多