Juan Carlos
Juan Carlos

Reputation: 187

The import org.springframework.test.context.junit4.SpringJUnit4ClassRunner cannot be resolved

I'm a newbie at Spring and this is also my very first question on StackOverflow so I'm going to try to make this as understandable as possible.

I'm trying to make a web service client using Spring and Maven on this tutorial: and I get this error: The import org.springframework.test.context.junit4 cannot be resolved

Here is my code:

package demo;

import hello.WsClientApplication;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //this won't import


@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WsClientApplication.class)
public class WsClientApplicationTests {

    @Test
    public void contextLoads() {
    }

}

Here is my pom.xml in case you need it.

<?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>org.springframework</groupId>
    <artifactId>gs-consuming-web-service</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
           <dependency>
               <groupId>org.springframework.ws</groupId>
               <artifactId>spring-ws-core</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- tag::wsdl[] -->
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.12.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>hello.wsdl</generatePackage>
                    <schemas>
                        <schema>
                            <url>http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl</url>
                        </schema>
                    </schemas>
                </configuration>
            </plugin>
            <!-- end::wsdl[] -->
        </plugins>
    </build>

</project>

I have tried some other solutions in StackOverflow but I can't get it to work.

Thanks.

Upvotes: 14

Views: 58556

Answers (7)

Ravi Bhanushali
Ravi Bhanushali

Reputation: 155

In case, you are not using the spring boot, but only spring framework you should add dependency for spring-test.

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

You can check the latest version for spring-test using mvn, and update accordingly.

If you are using spring-boot project than add spring boot dependency, if you are not using spring-boot the below will work, but not recommended.

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

NOTE: If you are using spring-boot project, adding <version> will not be necessary.

Upvotes: 0

Eduardo B
Eduardo B

Reputation: 29

Just to improve in the situation.

I managed to fix it like this:

before, spring boot initializr created a dependency to spring-boot-starter-test but with the exclusion, like this:

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Eclipse accused the annotation @Runwith to be Junit 4, therefore, the logic thing to do was to remove the exclusion.

Final POM dependency:

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

Upvotes: 2

Micheal
Micheal

Reputation: 391

I know this is answered kind of late, but I had the same problem and I found I was able to fix it 2 ways.

  1. I was able to remove the < scope > test < / scope> tags and that removed the restriction
  2. I had an issue with intellij not properly handling it when doing tests, so I made sure it was marked as test-root, I changed it from green -> gray -> back to the green test-root and that resolved my issue.

Upvotes: 1

Pompeu
Pompeu

Reputation: 1

sometimes maven start downloading dependencies and doesn't finish. Go to your .m2 folder and type:

find . -name *progre*

this command will show you every file with the tag "in-progess", which are the files missing or not finished.

delete the folder and try to update the dependencies again.

Upvotes: 0

Baker
Baker

Reputation: 28020

Gradle

In gradle this would be done by adding

testCompile("org.springframework.boot:spring-boot-starter-test")

to the dependencies block as in:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

After that it should be possible to write an import statement at the top of your class

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Allowing you to use the annotation:

@RunWith(SpringJUnit4ClassRunner.class)
public class MyAppTest {

}

Upvotes: 10

SekharKari
SekharKari

Reputation: 521

Now, if you are using latest spring-boot, 1.5.2 RELEASE, @SpringApplicationConfiguration is no longer available, instead you must use @SpringBootTest. Reference here(@spring boot starter test in Spring Boot)

Upvotes: 12

Wim Deblauwe
Wim Deblauwe

Reputation: 26858

You need to add a dependency on spring-boot-starter-test:

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

Upvotes: 26

Related Questions