When developing with Alfresco it is often necessary to use a Repository Action to execute some function against a document. For example to automatically apply some metadata values and move the document to a new folder based on its lifecycle. Information on the Action framework can be found here .
It is therefore importance to understand how to test actions as part of the development/release cycle in order to ensure that an untested action is never deploy into production.
This blog explains how you can test your actions using the junit library when developing using Alfresco Maven JDK.
Some of the many reason to test your action are:
- Decrease the risk of having bugs in the code
- Increase the speed of development, you don’t to deploying your code in alfresco and wait until the server is up and navigate making “click click click” to perform the action.
- You can stack all your tests and know if your changes break the code * Etc
How to test an action using junit
The first step is to setup your maven sdk development environment. This blog assumes you know how to do this. Once you have the mvn repository build working you can develop a junit and run it from eclipse through the run –> junit menu.
For this example, we have an action class “MyClass” that adds the geographic aspect, a junit test should check that the document includes the aspect once the action has completed.
We have created the following junit test class in the src/java/test maven package.
**Test Class**
@RunWith(RemoteTestRunner.class) @Remote(runnerClass = SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:alfresco/application-context.xml") public class MyClassTest { static Logger log = Logger.getLogger(MyClassTest.class); @Autowired protected TransactionService transactionService; @Autowired protected ActionService actionService; @Autowired protected NodeService nodeService; @Test public void myTest() throws Exception { // 1) String adminUserName = AuthenticationUtil.getAdminUserName(); AuthenticationUtil.setFullyAuthenticatedUser(adminUserName); RetryingTransactionHelper transactionHelper = transactionService.getRetryingTransactionHelper(); // 2) final NodeRef nodeRef = getNodeRefToTest(); // 3) transactionHelper.doInTransaction(new RetryingTransactionCallback<Void>() { public Void execute() throws Throwable { // 4) Action action = actionService.createAction("MI-CLASS-NAME-IN-SPRING"); actionService.executeAction(action, nodeRef); return null; } }); //EXAMPLE tests: // 5) assertTrue(nodeService.hasAspect(nodeRef, ContentModel.ASPECT_GEOGRAPHIC)); } .... }
Some Explanation of the items marked in the code
- Item one shows you how to assume the role of any user. You can test the result of a specific user running the action.
- getNodeRef to test is a helper function to get the node you want to test the action against. you can also create this node as part of your initialisation of the unit test and then delete it on teardown.
- This point is important the invocation of the action need to be done in a transaction if you intend to change the node in your action. This is not documented by Alfresco in their MVN development SDK.
- You create the action using the ActionService. The createAction function takes the spring name of the bean you have configured for the action in your context file.
- Finally, the assertTrue function tests if the action has applied the geographic aspect.
Some considerations
- For our testing we use “org.alfresco.util.TempFileProvider” to create temporary documents/nodes in alfresco as part of getNodeRefToTest. Alfresco will then destroy the temporary node for you after approximately an hour. This allows you to clean up you repository after your test without having to do it in a tear down.
- Using this method you can debug your code by adding breakpoints in the action.
Conclusion
Hopefully this helps you quickly develop tests for your alfresco actions when using the Alfresco JDK. With this continuous development approach you can test your actions quickly without the need to deploy and restart alfresco during development.