domain-specific testing tools lessons learned …... @rombert domain-specific testing tools...
TRANSCRIPT
http://robert.muntea.nu @rombert
Domain-Specific Testing Tools
Domain-Specific Testing ToolsLessons learned from the Apache Sling Project
Robert MunteanuApacheCon Core Europe 2015
http://robert.muntea.nu @rombert
Who I am
$DAYJOB Adobe Experience
Manager Apache Sling Apache Jackrabbit Apache Felix
FOSS Apache Sling MantisBT Mylyn Connector for
MantisBT Mylyn Connector for Review
Board
Speaker.currentSpeaker().interrupt();
http://robert.muntea.nu @rombert
Agenda
Testing tools in general Building domain-specific testing tools Domain-specific-tools built by Apache Sling
http://robert.muntea.nu @rombert
How do I test my application?
Testing and testing tools
http://robert.muntea.nu @rombert
Unit testing
@Test public void isNull() {
assertThat( StringUtils.isNull( null ), is(true));
}
http://robert.muntea.nu @rombert
Unit testing with mocks
@Test public void persist() {
MyDao dao = mock(MyDao.class);
MyService service = new MyService(dao);
service.persist(new ServiceObject()); // must not fail
}
http://robert.muntea.nu @rombert
Unit testing with mocks
@Test(expected=ServiceException.class)
public void persist() {
MyDao dao = mock(MyDao.class);
MyService service = new MyService(dao);
when(dao.persist(anyObject)).thenThrow(new DaoUnavailableException("mocked"));
service.persist(new ServiceObject());
}
http://robert.muntea.nu @rombert
Integration testing
@Test public void persist() {
MyDao dao = new MyRealDao(/* config */);
MyService service = new MyService(dao);
service.persist(newServiceObject()); // must not fail
}
http://robert.muntea.nu @rombert
Integration testing - clean slate
@Before public void ensureCleanSlate() {
MyDao dao = new MyRealDao(/* config */);
dao.deleteAll();
}
http://robert.muntea.nu @rombert
End-to-end testing
@Test public void login() {
Client client = new MyBrowserBasedClient();
AuthResult result = client.login("admin", "admin");
assertThat(result.isLoggedIn(), is(true));
}
http://robert.muntea.nu @rombert
OSGi
● Provision and deploy bundles● Configure, register and lookup services● Eventing● Web Console
http://robert.muntea.nu @rombert
Unit testing OSGi with Sling Mocks
public class ExampleTest {
@Rule public final OsgiContext context = new OsgiContext();
@Test public void testSomething() {
// register and activate service MyService service1 = context.registerInjectActivateService(new MyService(), ImmutableMap.<String, Object>of("prop1", "value1"));
// get service instance OtherService service2 = context.getService(OtherService.class); }}
http://robert.muntea.nu @rombert
Unit testing OSGi with the Humble Object Pattern
public interface RouterAdmin { void doStuff();}
public class RouterAdminImpl implements RouterAdmin { // constructor and field elided public void doStuff() { // implementation }}
@Component @Properties({ @Property(name="url") })public class RouterAdminComponent implements RouterAdmin { private RouterAdmin delegate;
protected void activate(ComponentContext ctx) throws Exception { delegate = new RouterAdminImpl(new URL(requireString(ctx, "url"))); } public void doStuff() { delegate.doStuff(); }}
http://robert.muntea.nu @rombert
Integration testing OSGi with Pax-Exam
public static Option[] paxConfig() { final File thisProjectsBundle = new File(System.getProperty( "bundle.file.name", "BUNDLE_FILE_NOT_SET" )); final String launchpadVersion = System.getProperty("sling.launchpad.version", "LAUNCHPAD_VERSION_NOT_SET"); log.info("Sling launchpad version: {}", launchpadVersion); return new DefaultCompositeOption( SlingPaxOptions.defaultLaunchpadOptions(launchpadVersion), CoreOptions.provision(CoreOptions.bundle(thisProjectsBundle.toURI().toString())) ).getOptions();}
http://robert.muntea.nu @rombert
Integration testing OSGi with Pax-Exam
@RunWith(PaxExam.class) public class FileNameExtractorImplIT {
@Inject private FileNameExtractor fileNameExtractor;
@Test public void testFileNameExtractor(){ String rawPath = "http://midches.com/images/uploads/default/demo.jpg#anchor?query=test"; String expectedFileName = "demo.jpg"; assertEquals(expectedFileName, fileNameExtractor.extract(rawPath)); }
@Configuration public Option[] config() { return U.paxConfig(); }}
http://robert.muntea.nu @rombert
JCR
blog
hello-world
images
jcr:content
some-cat.jpg
other-cat.jpg
http://robert.muntea.nu @rombert
JCR
- jcr:primaryType = app:asset- jcr:title = Some Cat- jcr:description = A longer description of this picture of a cat- jcr:created = 2014-06-03T00:00:00.000+02:00- jcr:lastUpdated = 2014-06-03T11:00:00.000+02:00- tags = [Animal, Cat, Color]- width = 400- height = 600
http://robert.muntea.nu @rombert
Unit testing JCR code
public class FindResourcesTest {
@Rule public SlingContext context = new SlingContext(ResourceResolverType.JCR_MOCK);
@Before public void setUp() { Resource resource = context.create().resource("test", ImmutableMap.<String, Object> builder().put("prop1", "value1") .put("prop2", "value2").build()); // snip ... MockJcr.setQueryResult(session, Collections.singletonList(node));}
@Test public void testFindResources() { Resource resource = context.resourceResolver().getResource("/test"); Assert.assertNotNull("Resource with name 'test' should be there", resource);
Iterator<Resource> result = context.resourceResolver().findResources("/test", Query.XPATH); Assert.assertTrue("At least one result expected", result.hasNext()); Assert.assertEquals("/test", result.next().getPath()); Assert.assertFalse("At most one result expected", result.hasNext()); }}
http://robert.muntea.nu @rombert
Better unit testing with Hamcrest matchers
@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));
}
http://robert.muntea.nu @rombert
Integration testing JCR code
● Unit testing with JCR Mocks● JCR_MOCK● SLING_MOCK
● Integration testing with JCR Mocks● JCR_JACKRABBIT● JCR_OAK
http://robert.muntea.nu @rombert
Server-side testing
public class ServerSideInstallerTest {
@Rule public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "Launchpad");
@Before
public void setup() throws LoginException {
ip = teleporter.getService(InfoProvider.class);
is = ip.getInstallationState();
}
@Test
public void noUntransformedResources() {
final List<?> utr = is.getUntransformedResources();
if(utr.size() > 0) {
fail("Untransformed resources found: " + utr);
}
}
}
http://robert.muntea.nu @rombert
Scriptable Server-side testing
// test imports that fail on Java 8, SLING-3405
%><%@page import="java.util.Arrays"%><%
%><%@page import="java.lang.CharSequence"%><%
%>TEST_PASSED
http://robert.muntea.nu @rombert
Sling testing tools cross-reference
OSGi JCR Sling
Unit TestingSling (OSGi) mocksHamcrest Matchers
Sling (JCR) MocksHamcrest Matchers
Sling MocksHamcrest Matchers
Integration testing Pax-Exam || Humble Object || Server-Side tests
End-To-End testing Sling HTTP Testing Tools
http://robert.muntea.nu @rombert
Final thoughts
● Identify what is particular to your application/product● Try to be testing tool agnostic● Recognise that different organisations test in different ways
http://robert.muntea.nu @rombert
Resources
●http://sling.apache.org●https://sling.apache.org/documentation/development/sling-mock.html
●http://jackrabbit.apache.org/
http://robert.muntea.nu @rombert
Colophon
Images● Tools IMG_0171 by OzinOH on Flickr● One Way Una Via by David Amsler on Flickr● Slingshots by by Anne and Tim on Flickr
World cloud generated with https://www.jasondavies.com/wordcloud/