如何解决此问题(类<;SpringJUnit4ClassRunner>;无法解析为类型)?

如何解决此问题(类<;SpringJUnit4ClassRunner>;无法解析为类型)?,spring,maven,Spring,Maven,我使用spring和maven实现了一个简单的项目,我的项目包含一个接口、3个类和pom.xml文件 接口:CompactDisc.java 3类: 实现CompactDisc接口的SgtPeppers.java CDPlayersConfig.java是一个空类,包含自动扫描的注释 CDPlayersTest.java是一个测试类,用于测试spring容器是否工作 我的问题是@RunWith(SpringJUnit4ClassRunner.class)和@ContextConfiguratio

我使用spring和maven实现了一个简单的项目,我的项目包含一个接口、3个类和pom.xml文件

接口:
CompactDisc.java

3类: 实现
CompactDisc
接口的
SgtPeppers.java

CDPlayersConfig.java
是一个空类,包含自动扫描的注释

CDPlayersTest.java
是一个测试类,用于测试spring容器是否工作

我的问题是@RunWith(SpringJUnit4ClassRunner.class)和@ContextConfiguration(classes=CDPlayersConfig.class),Eclipse建议第一个注释的这个命题类不能解析为一个类型,它不理解第二个注释

您可以在这里找到代码:

CompactDisc.java

     package soundsystem; 
    public interface CompactDisc {
        void play();
}
package soundsystem;

import org.springframework.stereotype.Component;

@Component
public class SgtPeppers implements CompactDisc{

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    public void play() {

        System.out.println("Playing " + title + "by"+ artist);
    }

}
package soundsystem;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class CDPlayersConfig {

}
package soundsystem;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.jta.SpringJtaSynchronizationAdapter;

@RunWith(SpringJUnit4ClassRunner.class)//Here is my problem
@ContextConfiguration(classes=CDPlayersConfig.class)//Here is my problem
public class CDPlayerTest {

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }
}
sgtpepers.java

     package soundsystem; 
    public interface CompactDisc {
        void play();
}
package soundsystem;

import org.springframework.stereotype.Component;

@Component
public class SgtPeppers implements CompactDisc{

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    public void play() {

        System.out.println("Playing " + title + "by"+ artist);
    }

}
package soundsystem;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class CDPlayersConfig {

}
package soundsystem;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.jta.SpringJtaSynchronizationAdapter;

@RunWith(SpringJUnit4ClassRunner.class)//Here is my problem
@ContextConfiguration(classes=CDPlayersConfig.class)//Here is my problem
public class CDPlayerTest {

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }
}
CDPlayersConfig.java

     package soundsystem; 
    public interface CompactDisc {
        void play();
}
package soundsystem;

import org.springframework.stereotype.Component;

@Component
public class SgtPeppers implements CompactDisc{

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    public void play() {

        System.out.println("Playing " + title + "by"+ artist);
    }

}
package soundsystem;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class CDPlayersConfig {

}
package soundsystem;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.jta.SpringJtaSynchronizationAdapter;

@RunWith(SpringJUnit4ClassRunner.class)//Here is my problem
@ContextConfiguration(classes=CDPlayersConfig.class)//Here is my problem
public class CDPlayerTest {

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }
}
CDPlayerTest.java

     package soundsystem; 
    public interface CompactDisc {
        void play();
}
package soundsystem;

import org.springframework.stereotype.Component;

@Component
public class SgtPeppers implements CompactDisc{

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    public void play() {

        System.out.println("Playing " + title + "by"+ artist);
    }

}
package soundsystem;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class CDPlayersConfig {

}
package soundsystem;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.jta.SpringJtaSynchronizationAdapter;

@RunWith(SpringJUnit4ClassRunner.class)//Here is my problem
@ContextConfiguration(classes=CDPlayersConfig.class)//Here is my problem
public class CDPlayerTest {

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }
}
pom文件中的依赖项

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
</dependency>

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.0.5.RELEASE</version>
            <scope>test</scope>
</dependency>

朱尼特
朱尼特
${junit.version}
测试
org.springframework
弹簧试验
4.0.5.1发布
测试

您缺少SpringJUnit4ClassRunner和ContextConfiguration的导入(导入org.springframework…)。这就是它们无法识别的原因。

您缺少SpringJUnit4ClassRunner和ContextConfiguration的导入(导入org.springframework…)。这就是为什么他们不被认出来。

我看不出来

 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.ContextConfiguration;
在您的导入部分。另外,也许您应该使用SpringRunner.class而不是SpringJUnit4ClassRunner.class

我看不出来

 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.ContextConfiguration;
在您的导入部分。另外,也许您应该在Eclipse中使用SpringRunner.class而不是SpringJUnit4ClassRunner.class

右键单击您的项目

选择Maven

选择更新项目

滴答声

从pom.xml更新项目配置

刷新工作区

清理项目

完成此操作后,Eclipse应该可以使用所需的库,并且您应该能够根据需要导入类。

在Eclipse中

右键单击您的项目

选择Maven

选择更新项目

滴答声

从pom.xml更新项目配置

刷新工作区

清理项目


完成此操作后,Eclipse应该可以使用所需的库,并且您应该能够根据需要导入类。

Control+单击该类。Eclipse将打开该文件。复制包名。创建一个新的导入语句并粘贴包名+类名。问题解决了


如果在control+单击时Eclipse没有打开该文件,则类路径上没有该文件,也没有正确导入该文件

Control+单击该类。Eclipse将打开该文件。复制包名。创建一个新的导入语句并粘贴包名+类名。问题解决了


如果在control+单击时Eclipse没有打开该文件,则类路径上没有该文件,也没有正确导入该文件

您必须添加依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.9.RELEASE</version> 
</dependency>    

您必须添加依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.9.RELEASE</version> 
</dependency>    

当我单击按钮导入包时,它给了我这样一条消息:类不能解析为类型我不确定这个按钮,因为我不太使用Eclipse。您是否尝试从“我的答案到导入”部分复制粘贴导入?是的,我尝试复制您的建议,但仍然存在相同的错误。然后,确定。您有spring测试版本4.0.5,而最新版本是5.0.6。你必须使用版本4吗?如果不是,我建议替换pom.xml中的版本。另外,看到整个pom.xml也很酷。由于您发布了其他依赖项,对吗?我将spring test的版本从4更改为5,但仍然存在相同的问题,当我单击按钮导入包时,它给了我以下消息:类无法解析为类型我不确定该按钮,因为我不太使用Eclipse。您是否尝试从“我的答案到导入”部分复制粘贴导入?是的,我尝试复制您的建议,但仍然存在相同的错误。然后,确定。您有spring测试版本4.0.5,而最新版本是5.0.6。你必须使用版本4吗?如果不是,我建议替换pom.xml中的版本。另外,看到整个pom.xml也很酷。由于您发布了其他依赖项,对吗?我将spring test的版本从4更改为5,但仍然存在相同的问题,当我单击按钮导入包时,它会给我以下消息:当我单击按钮导入包时,类无法解析为类型,它给了我这样一个信息:类不能被解析为类型这个解决方案是不正常的,但它对我也有效。看起来STS有些问题。Ctrl+1无法自动解决此问题。是的,这不正常,这是一种STS缺陷解决方法,适用于需要在截止日期前解决的情况!:)这个解决方案不正常,但对我来说也很有效。看起来STS有些问题。Ctrl+1无法自动解决此问题。是的,这不正常,这是一种STS缺陷解决方法,适用于需要在截止日期前解决的情况!:)