Spring boot SpringBoot-Can';t解析@RunWith-找不到符号

Spring boot SpringBoot-Can';t解析@RunWith-找不到符号,spring-boot,build.gradle,Spring Boot,Build.gradle,SpringBoot项目 在build.gradle中: dependencies { implementation 'com.google.code.gson:gson:2.7' implementation 'com.h2database:h2' implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:sprin

SpringBoot项目

在build.gradle中:

dependencies {
    implementation 'com.google.code.gson:gson:2.7'
    implementation 'com.h2database:h2'
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
    implementation('com.squareup.retrofit2:retrofit:2.4.0')
    implementation('com.squareup.retrofit2:converter-gson:2.4.0')
    implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'

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

test {
    useJUnitPlatform()
}
这是我的测试课:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

@RunWith(SpringRunner.class)
@DataJpaTest
public class CategoryRepositoryIntegrationTest {
    @Autowired
    private TestEntityManager entityManager;
    @Autowired
    private CategoryRepository productRepository;
但我得到了一个错误:

error: cannot find symbol

@RunWith(SpringRunner.class)
 ^
  symbol: class RunWith
1 error
FAILURE: Build failed with an exception

你混合了JUnit4和JUnit5。您使用来自JUnit5的测试注释,而RunWith注释来自JUnit4。我建议使用JUnit5。为此,只需将RunWith替换为以下行:

@ExtendWith(SpringExtension.class)

或者,如果您使用SpringBoot 2.1或更高版本,您可以删除RunWith注释,它也应该可以工作。

我从SpringBoot文档中找到了这个,不确定是否有助于解决您的问题

如果您使用的是JUnit4,请不要忘记将@RunWith(SpringRunner.class)添加到测试中,否则注释将被忽略。如果您使用的是JUnit 5,则无需将等效的@ExtendWith(SpringExtension.class)添加为@SpringBootTest,其他@Test注释已经用它进行了注释

这里是链接 并检查第46.3节