gpt4 book ai didi

java - Alfresco ServiceRegistry 无法注入(inject) Junit 中

转载 作者:行者123 更新时间:2023-12-02 11:38:17 26 4
gpt4 key购买 nike

我正在尝试为我的 Alfresco SDK 应用创建一个测试环境。我需要测试我创建的一个服务,该服务具有 ServiceRegistry 作为属性,由 Spring 通过构造函数注入(inject)。如果应用程序正在运行,则服务运行良好。

对于 bean 声明,我使用注释:

@Service(value = "customFormService")
public class CustomFormService implements FormService<String> {
private final ServiceRegistry serviceRegistry;

@Autowired
public CustomFormService(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}

在服务应用程序上下文中:

/amp/config/module/sampleapp/context/service-context.xml

我声明<context:component-scan base-package="com.base"/>启用带注释的组件扫描

问题是当我尝试创建这样的单元测试时:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations = {"classpath*:config/alfresco/module/sampleapp/context/service-context.xml"}
)
public class DocumentManagementTest {
@Autowired
@Qualifier(value = "customFormService")
FormService<String> customFormService;

@Test
public void testWiring() {
Assert.assertNotNull(customFormService);
}
}

我遇到异常:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.base.form.service.FormService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=customFormService}

令人非常困惑的是,当应用程序运行时注入(inject)工作正常,但在 Junit 测试用例中却失败了。

最佳答案

如果您想在类中注入(inject)ServiceRegistry,那么您需要添加属性

 <bean id="reject-content" class="com.repo.RejectContent"
parent="action-executer">
<property name="serviceRegistry">
<ref bean="ServiceRegistry" />
</property>


</bean>

像上面一样到你的bean。com.repo.RejectContent在 RejectContent 类中,您需要为服务注册表创建 setter,如下所示:

private ServiceRegistry serviceRegistry;

public void setServiceRegistry(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}

之后您就可以使用serviceRegistry

关于java - Alfresco ServiceRegistry 无法注入(inject) Junit 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48766443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com