从Alfresco集成测试上下文访问Spring bean

时间:2019-05-28 15:22:02

标签: java spring integration-testing alfresco

现在,自定义模块的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的集成测试时使用的方法吗?

使用Maven 4.0.0 SDK原型的模块构建中至少不再包含注释org.springframework.test.context.ContextConfiguration

This blog post讨论上述注释。但是从SDK 4原型创建的pom.xml引入的依赖项不包括这些注释。

似乎仅使用另一种方法

@RunWith(value = AlfrescoTestRunner.class)

关于集成测试类。但是如何将像nodeService这样的Spring bean注入其中呢?以及我该如何声明和提供其他Spring Bean,这些Spring Bean是我的自定义模块的一部分,并且集成测试需要成功才能实现?

1 个答案:

答案 0 :(得分:3)

您可以通过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上尝试过。

相关问题