首页 > 解决方案 > 使用 IntelliJ 在 Windows 10 上使用 gradle 依赖项导入失败

问题描述

我正在关注 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 上。

标签: javaspring-bootgradleintellij-idea

解决方案


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

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


推荐阅读