Georgi Michev
Georgi Michev

Reputation: 894

SpringBootTest annotation cannot be resolved to a type

My SpringBootTest annotation cannot be resolved to a type. There is the same question here but it seems that adding the dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.0.5.RELEASE</version>
    <scope>test</scope>
</dependency>

cannot do the trick. Whole problem is that I wanted to use @ContextConfiguration but it is deprecated and suggested way of doing things is using the @SpringBootTest(classes = MyMainClass.class)

Upvotes: 9

Views: 29465

Answers (5)

Semo
Semo

Reputation: 821

Kudos to @kj007. If anyone searches for an answer on "gradle", then this line might be for you:

    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
testImplementation 'org.springframework.boot:spring-boot-starter-test:3.2.0'

"It worked on my Computer" ™

Upvotes: 1

MAYOBYO HASSAN
MAYOBYO HASSAN

Reputation: 506

After removing the <scope>, i was able to fix my problem

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.2.2.RELEASE</version>
<!-- <scope>test</scope> -->
    </dependency>

Upvotes: 28

kj007
kj007

Reputation: 6254

Version not required to define in test dependency if you are using parent starter, this might be conflicting in loading.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

so complete pom would be like this..

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>

    <groupId>com.kj</groupId>
    <artifactId>demo-multi-schema</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Upvotes: 4

Karol Katanowski
Karol Katanowski

Reputation: 186

I would try to use @RunWith annotation and @Test in Spring Test class here i have an example:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class YourClassAppTest {

    @Test
    public void contextLoads() {
    }

}

hope it will solve the problem

Upvotes: 0

萝莉w
萝莉w

Reputation: 177

Try to use the dependency 2.0.3. I am using this version now.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.0.3.RELEASE</version>
    <!--<scope>test</scope>-->
</dependency>

Upvotes: 4

Related Questions