Getting Started: The SlingClient - adamcin/graniteit-maven-plugin GitHub Wiki

Step 1: Add test dependencies:

Edit /content/pom.xml and add the following dependencies under junit:

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.testing.tools</artifactId>
    <version>1.0.6</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>net.adamcin.commons</groupId>
    <artifactId>net.adamcin.commons.testing</artifactId>
    <version>0.8.0</version>
    <scope>test</scope>
  </dependency>

  ... [other dependencies in /content/pom.xml] ...
</dependencies>

Step 2: Create a new class under /content/src/test/java called CreateResourceIT.java:

import net.adamcin.commons.testing.junit.FailUtil;
import net.adamcin.commons.testing.sling.SlingRemoteTestContext;
import org.apache.sling.testing.tools.sling.SlingClient;
import org.junit.Assert;
import org.junit.Test;

public class CreateResourceIT {

    SlingRemoteTestContext context = new SlingRemoteTestContext();

    @Test
    public void createResource() {

        SlingClient client = context.getSlingClient();

        final String path = "/tmp/deleteMe";

        try {
            Assert.assertFalse(path + " should not exist yet", client.exists(path));
            client.createNode(path, "jcr:primaryType", "nt:unstructured");
            Assert.assertTrue(path + " should exist after creation", client.exists(path));
        } catch (Exception e) {
            FailUtil.sprintFail(e);
        } finally {
            try {
                client.delete(path);
            } catch (Exception ignored) {  }
        }
    }
}
⚠️ **GitHub.com Fallback** ⚠️