微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JUnit中的Service类的NoSuchBeanDefinitionException

如何解决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 举报,一经查实,本站将立刻删除。