Integration Tests in Adobe CQ

Published on by Dan KlcoPicture of Me Dan Klco

Awhile back I wrote a post on running integration tests in Apache Sling. This technique is useful for developers working directly on Apache Sling, but doesn’t support downstream platforms like Adobe CQ/AEM.

After some finagling and testing I was able to get a similar technique working using Adobe CQ / AEM instead of Apache Sling. To enable integration tests using Adobe CQ, add the following into your POM.


First, add plugins to copy required dependencies and the current project output into the CQ instance:

<!-- Copy the required dependencies -->
<!-- Copy the project output -->
					<copy file="${}/${}.jar" toDir="${}/sling/additional-bundles" verbose="true" />

The artifact id’s specified must be dependencies of the current project to be copied.

Failsafe Configuration

Next, add a similar failsafe configuration to the one in the Apache Sling integration tests:

			<jar.executor.jar.options>-p $JAREXEC_SERVER_PORT$ -nobrowser -nofork</jar.executor.jar.options>
			<jar.executor.vm.options>-XX:MaxPermSize=256m -Xmx1024M</jar.executor.vm.options>

The changes here to note, is that this will be looking for a JAR with a name like cq5-{SOMETHING}.jar in the directory specified by the cq.dir property. This property should be set as a command line parameter so that different environments can be easily supported. The test configuration will start the CQ with enough memory, suppress the new browser window and prevent CQ from forking the process. The server.ready.path.1 property is set to the welcome page for CQ 5.6.1, if you are using a different version of CQ, this URL may need to change.

At this point, when you run the build, a CQ5 instance will be automatically started, any test classes with names like {SOMETHING}IT will be run and the CQ instance will be stopped.


One odd thing I have noticed is that when uploading files, CQ sometimes returns 201 and sometimes returns 200. This causes errors when trying to upload files with the SlingClient, to get around this, I created a class CQClient, which allows uploads if 200 or 201 is returned:

package com.myco.project.test;


import junit.framework.Assert;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class CQClient extends SlingClient {

	private RequestBuilder builder;
	private RequestExecutor executor;
	private String username;
	private String password;

	public CQClient(String slingServerUrl, String username, String password) {
		super(slingServerUrl, username, password);
		builder = new RequestBuilder(slingServerUrl);
		executor = new RequestExecutor(new DefaultHttpClient());
		this.username = username;
		this.password = password;

	 * Upload using a PUT request.
	 * @param path
	 *			the path of the uploaded file
	 * @param data
	 *			the content
	 * @param length
	 *			Use -1 if unknown
	 * @param createFolders
	 *			if true, intermediate folders are created via mkdirs
	public void upload(String path, InputStream data, int length, boolean createFolders) throws IOException {
		final HttpEntity e = new InputStreamEntity(data, length);
		if (createFolders) {
		HttpResponse response = executor.execute(
			builder.buildOtherRequest(new HttpPut(builder.buildUrl(path)))
					.withEntity(e).withCredentials(username, password))
		int status = response.getStatusLine().getStatusCode();
		Assert.assertTrue("Expected status code 201 or 202, received: "+status,status == 201 || status == 200);

From this point on, the integration test works exactly the same as the integration tests for Apache Sling.

Debugging Tests

If you are having issues with the tests and you want to do some debugging within your IDE, you can execute a Maven build like the following:

mvn clean install -Dmaven.failsafe.debug test -Pcq.dir=/Users/username/cq/5.6.1 -PforkMode=never

This will prevent maven from forking and allow you to debug through your integration test class execution.


You can find an example of a working Adobe CQ / AEM integration test in the Component Bindings Provider project simply clone the project and call mvn clean install -Pcq.dir={SOME_CQ_DIRECTORY} inside the project directory to see the integration tests execute.


comments powered by Disqus