JUnit中的Service类的NoSuchBeanDefinitionException

问题描述

TestClass.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/WebContent/WEB-INF/application-context.xml" })
public class TestClass{

    public static FileUploadContextAware uploadContextAware;

    @BeforeClass
    public static void configure() {
        ApplicationContext context = new ClasspathXmlApplicationContext(
                "classpath*:/WebContent/WEB-INF/application-context.xml");
        uploadContextAware = new FileUploadContextAware();
        uploadContextAware.setApplicationContext(context);
        IFileUploadService fileUploadService = (IFileUploadService) FileUploadContextAware.getBean("fileUploadService");
        
    }
../Actual test code

FileUploadServiceImpl.class:

@Service("fileUploadService")
public class FileUploadServiceImpl implements IFileUploadService {
     ..//more code
}

application-context.xml:

<context:component-scan
        base-package="com.basepackage.*" />

我尝试在application-context.xml中将 bean 添加为:

bean id="fileUploadService" name="fileUploadService" class="com.basepackage.upload.FileUploadServiceImpl" 
        /> 

但是,当我运行测试时,会为名称 fileUploadService 的bean抛出 NoSuchBeanDeFinitionException 。知道为什么该bean在上下文容器中不可用吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)