Apache
Home » Documentation » Development

Hamcrest Integration

Deeper integration with the Hamcrest matcher library.

Maven Dependency

This module has not yet been officially released.
1
2
3
4
<dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.testing.hamcrest</artifactId>
</dependency>

Note that to keep the classpath consistent this module has all its dependencies marked as provided. It relies on your own project to define the needed dependencies, such as org.hamcrest:hamcrest-core and org.apache.sling:org.apache.sling.api .

See latest version on the downloads page.

Usage

The class org.apache.sling.testing.hamcrest.ResourceMatchers is the main entry point. It contains static methods that can be used to create assertions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import static org.apache.sling.hamcrest.ResourceMatchers.resourceOfType;

public void MyServiceTest {

  @Test 
  public void loadResources() {
    Map<String, Object> expectedProperties = /* define properties */;
    Resource resource = /* load resource */ null;

    assertThat(resource, resourceOfType("my/app"));
    assertThat(resource, hasChildren("header", "body"));
    assertThat(resource, resourceWithProps(expectedProperties));
  }

}

The Slingshot sample application uses these matchers, see SetupServiceTest.java for an example.

Rev. 1705523 by rombert on Sun, 27 Sep 2015 10:15:02 +0000
Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.