SpringBoot - Can't resolve @RunWith - cannot find symbol

浪尽此生 提交于 2021-01-05 11:33:22

问题


SpringBoot project.

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

Here my test class:

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;

But I get error:

error: cannot find symbol

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

回答1:


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)

Or if you use SpringBoot 2.1 or older, you can remove the RunWith annotation and it should also work.



来源:https://stackoverflow.com/questions/63834940/springboot-cant-resolve-runwith-cannot-find-symbol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!