既然自定义模块的 Alfresco 集成测试是使用 Docker 运行的,我想知道如何在此上下文中提供额外的 Spring bean 以及如何在测试类中访问现有的 Spring bean。
直到Alfresco 5.x,我习惯用注释测试类
@ContextConfiguration("classpath:alfresco/application-context.xml")
这使得 Spring 上下文可用。为了使来自此上下文的 Spring bean 在测试类中可用,我对成员进行了如下注释:
@Autowired
@Qualifier("authenticationComponent")
private AuthenticationComponent authenticationComponent;
此外,我还能够在 src/test/resources/alfresco/extension/test-context.xml
中定义额外的 Spring bean。
这是为 6.x 和 Docker 编写集成测试时使用的方法吗?
至少注释 org.springframework.test.context.ContextConfiguration
不再包含在使用 Maven 4.0.0 SDK 原型(prototype)的模块构建中。
This blog post谈论上面提到的注释。但是从 SDK 4 原型(prototype)创建的 pom.xml
引入的依赖项不包含这些注释。
另一种方法似乎是只使用
@RunWith(value = AlfrescoTestRunner.class)
关于集成测试类。但是我如何将 nodeService
之类的 Spring bean 注入(inject)其中呢?我如何声明和提供额外的 Spring bean,它们是我的自定义模块的一部分并且是集成测试成功所必需的?
最佳答案
您可以通过 AlfrescoTestRunner
获取 Spring 上下文,如下所示:
@Before
public void setUp() {
this.nodeService = (NodeService) super.getApplicationContext().getBean("nodeService");
}
我对自定义 bean 做同样的事情:
super.getApplicationContext().getBean(MyType.class);
由于集成测试在存储库中运行,因此所有 Spring 上下文自动可用。
请注意,您的测试类需要扩展 AbstractAlfrescoIT
才能正常工作。
一个示例类可能如下所示:
package nl.open.mystuff;
import org.alfresco.rad.test.AbstractAlfrescoIT;
import org.alfresco.rad.test.AlfrescoTestRunner;
import org.alfresco.service.cmr.repository.NodeService;
@RunWith(value = AlfrescoTestRunner.class)
public class MyCustomIT extends AbstractAlfrescoIT {
private NodeService nodeService;
private MyType myType;
@Before
public void setUp() {
this.nodeService = (NodeService) super.getApplicationContext().getBean("NodeService");
this.myType = super.getApplicationContext().getBean(MyType.class);
}
}
在 Alfresco SDK 3 中,您甚至可以在 src/test/resources/alfresco/extension/*-context.xml
下添加您自己的 Spring XML 文件。我想这仍然有效,但我自己还没有用 SDK 4 尝试过。
关于java - 从 Alfresco 集成测试上下文访问 Spring bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56345430/