本文介绍了SpringBoot-无法解析@RunWith-无法找到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SpringBoot项目.
SpringBoot project.
在build.gradle中:
In 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
推荐答案
您混合使用了JUnit 4和5.您使用了JUnit 5中的Test注释,而RunWith注释来自JUnit4.我建议您使用JUnit 5.您只需要用以下行替换RunWith:
You mixed JUnit 4 and 5. You use the Test annotation from JUnit 5 and the RunWith annotation is from JUnit 4. I would recommend using JUnit 5. For this you only need to replace RunWith with the following line:
@ExtendWith(SpringExtension.class)
或者,如果您使用的是SpringBoot 2.1或更早版本,则可以删除RunWith批注,它也应该起作用.
Or if you use SpringBoot 2.1 or older, you can remove the RunWith annotation and it should also work.
这篇关于SpringBoot-无法解析@RunWith-无法找到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!