Access Spring beans from Alfresco integration test context

user11044402 :

Now that Alfresco integration tests of custom modules are run using Docker, I wonder how to make additional Spring beans available in this context and how to access existing Spring beans in test classes.

Until Alfresco 5.x, I used to annotate the test class with

@ContextConfiguration("classpath:alfresco/application-context.xml")

This made the Spring context available. To make Spring beans from this context available in the test class, I annotated members like this:

@Autowired
@Qualifier("authenticationComponent")
private AuthenticationComponent authenticationComponent;

In addition I was able to define additional Spring beans in src/test/resources/alfresco/extension/test-context.xml.

Is this the approach to use when writing integration tests for 6.x and Docker?

At least the annotation org.springframework.test.context.ContextConfiguration is no longer included in a module build using the Maven 4.0.0 SDK archetype.

This blog post talks about the above mentioned annotations. But the dependencies pulled in by the pom.xml created from the SDK 4 archetype don't include these annotations.

A different approach seems to be to only use

@RunWith(value = AlfrescoTestRunner.class)

on the integration test class. But how do I get the Spring beans like nodeService injected into it? And how do I declare and make available additional Spring beans which are part of my custom module and required by the integration test to succeed?

Mark Tielemans :

You can get the Spring context via AlfrescoTestRunner as follows:

@Before
public void setUp() {
    this.nodeService = (NodeService) super.getApplicationContext().getBean("nodeService");
}

I do the same with custom beans: super.getApplicationContext().getBean(MyType.class);

Since the integration tests run in the repository, all of the Spring context is automatically available.

Note that your test class needs to extend AbstractAlfrescoIT for this to work.

An example class may look like this:

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);
    }
}

In Alfresco SDK 3, you can even add your own Spring XML files under src/test/resources/alfresco/extension/*-context.xml. I imagine this still works, but I haven't tried it with SDK 4 myself.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326279&siteId=1