Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 553
Alexei Avatar asked Dec 14 '22 08:12

Alexei


1 Answers

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.

like image 186
flaxel Avatar answered Jan 09 '23 23:01

flaxel