Documente Academic
Documente Profesional
Documente Cultură
AEM Developer
Apache Sling PMC
CTO of pro!vision GmbH
https://www.pro-vision.de
adaptTo() 2016 2
Recap: Sling & AEM Mocks
adaptTo() 2016 3
What is Sling Mocks, AEM Mocks & Co
@Rule
public final SlingContext context = new SlingContext();
@Test
public void testSomething() {
Resource resource = context.resourceResolver().getResource("/content/sample/en");
// further testing
} context.bundleContext()
context.componentContext()
} context.resourceResolver()
context.currentResource()
context.request()
public class ExampleAemTest { context.requestPathInfo()
context.response()
@Rule context.slingScriptHelper()
public final AemContext context = new AemContext(); context.runMode("author");
... context.pageManager()
...
adaptTo() 2016 5
Choose Resource Resolver implementation
more features
Type API API Types vation Query Fulltext
RESOURCE
FASTER
JCR_MOCK
(mocked)
JCR_OAK
adaptTo() 2016 6
Sling Mocks Features
adaptTo() 2016 7
AEM Mocks Features
adaptTo() 2016 8
New Features
adaptTo() 2016 9
OSGi Companion services
EventAdmin
Send and receive OSGi events
ConfigAdmin
Provide OSGi configurations at runtime
adaptTo() 2016 10
OSGi R6 support
OSGi R6 API
Field-based reference bindings
Support collections of services and references
Component property types for config
Annotation class-based configuration
adaptTo() 2016 11
OSGi R6 support
@Component(service = MyService.class, immediate = true)
@Designate(ocd = MyServiceImpl.Config.class)
public class MyServiceImpl implements MyService {
@Activate
private void activate(Config config) {
// Process configuration
}
Supported by
} OSGi Mocks
adaptTo() 2016 12
Automatic JCR Node Type registration
adaptTo() 2016 13
Simple mocking of Adapter Factories
adaptTo() 2016 14
New AEM Mocks Features
adaptTo() 2016 15
Asset and rendition creation
// create asset from byte array
asset = context.create().asset("/content/dam/sample1.bin",
new ByteArrayInputStream(new byte[] { 0x01, 0x02, 0x03 }),
"application/octet-stream");
adaptTo() 2016 16
Simplified properties/map handling
// after
resource = context.create().resource("/content/test1/resource2",
"jcr:title", "Test Title",
"intProp", 5);
adaptTo() 2016 17
ResourceBuilder integration
Result /content
/site1
@prop1 = "value1“
/en
/page1
@jcr:title = "My title"
/page2
adaptTo() 2016 18
Version Matrix
adaptTo() 2016 19
API Support Matrix
OSGi R4, R5 R6
adaptTo() 2016 20
AEM Version Usage Matrix
adaptTo() 2016 21
Pitfalls
adaptTo() 2016 22
#1 Correct versions
adaptTo() 2016 23
#2 Unobfuscated APIs
adaptTo() 2016 24
#3 Transitive dependency mismatch
adaptTo() 2016 26
#3 Transitive dependency mismatch
Possible solutions/workarounds
Exclude mismatches from other deps
Change dependency order (move mocks to top)
Inherit from parent POM which declares all
correct versions for each AEM major version,
e.g. http://wcm.io/tooling/maven/aem-
dependencies.html
adaptTo() 2016 27
#4 Eclipse Gotcha: No SCR metadata
Mock Documentation
Sling Mocks: http://sling.apache.org/documentation/development/sling-mock.html
OSGi Mocks: http://sling.apache.org/documentation/development/osgi-mock.html
JCR Mocks: http://sling.apache.org/documentation/development/jcr-mock.html
ResourceResolver Mocks: http://sling.apache.org/documentation/development/resourceresolver-mock.html
AEM Mocks: http://wcm.io/testing/aem-mock/
adaptTo() 2016 30
References
adaptTo() 2016 31
Questions?
adaptTo() 2016 32