www.openweb.nl
Unit Testing Hippo
By Ebrahim Aharpour
CTO and Software Engineer at Openweb
ebrahim@openweb.nl
www.openweb.nl
Why do we write unit
tests?
www.openweb.nl
Why do we write unit tests?
▰ To ensure better quality
▰ To improve productivity
▰ To increase maintainability
333
www.openweb.nl
What are the attributes of
an Ideal unit test?
www.openweb.nl
Attributes of an ideal unit test
▰ Independent
▰ Repeatable
▰ Maintainable (Easy to read and Modify)
▰ Only test a unit of code
▰ Fast
▰ Stable
555
www.openweb.nl
“Stable: The test only fails,
when the code under test
doesn't behave as
expected, not because
anything else changed.
Jens Schauder
Spring Data team member
666
www.openweb.nl
Example
Replacing the following line
Node subnode = node.getNode(“parent”).getNode(“subnode”);
With
Node subnode = node.getNode(“parent/subnode”);
In a class should not break the unit tests. Because according JCR
specifications these two lines always success with the same result or fails
with the same error.
777
www.openweb.nl
What is the solution?
www.openweb.nl
Overview
▰ Mock JCR Implementations
▻ In memory JCR
▰ JCR Mocking Tool
▰ Hippo Unit Tester
999
www.openweb.nl
What are the Mock JCR solutions
available?
▰ Mockito based Solutions
▻ JCR Mock
▻ JCR Mock Up
▰ Object Mock based Solutions
▻ Apache Sling JCR Mock
▻ Magnolia Test Suite
▰ Jackrabbit based Solutions
▻ Jackrabbit
▻ In Memory JCR
101010
www.openweb.nl
Overview
▰ Mock JCR Implementations
▻ In memory JCR
▰ JCR Mocking Tool
▰ Hippo Unit Tester
111111
www.openweb.nl
In Memory JCR
www.openweb.nl
Introduction
In Memory JCR is a lightweight wrapper
around Jackrabbit that makes setting up and
tearing down a JCR repository much easier
and provide tooling for mock registration of
node types and namespaces without hassle.
In Memory JCR keeps all the data and indexes
in memory but still it places a few XML files in
a Temporary folder which get deleted on
shutdown. So make sure to shut down (or
close) during the teardown phase. 131313
www.openweb.nl
Pros
▰ Fully compliant to JCR Specs
▰ Support importing node structure as
document or system view XML
▰ Easy to setup and teardown
▰ Provide utilities for registering namespaces and
node types
▰ Licensed Under Apache License 2.0
141414
www.openweb.nl
Cons
▰ It is rather slow (~250ms per test) in comparison
with the other solutions (it is ~10 slower than
Mockito based solution like “JCR Mock” and
“JCR Mock Up” and ~100 times slower than
“Magnolia test” and “Sling”)*
▰ Note types need to be register although there
are utilities that make it trivial
151515
www.openweb.nl
Usage
161616
www.openweb.nl
Usage
171717
www.openweb.nl
Overview
▰ Mock JCR Implementations
▻ In memory JCR
▰ JCR Mocking Tool
▰ Hippo Unit Tester
181818
www.openweb.nl
JCR Mocking Tool
www.openweb.nl
Introduction
JCR Mocking tool is an attempt to make it easier to
setup a mock JCR repository via JSON or XML using
any given JCR mock implementation as long is it
support programmatic creation of nodes and
properties.
202020
www.openweb.nl
What are the Mock JCR solutions
available?
▰ Mockito based Solutions
▻ JCR Mock
▻ JCR Mock Up
▰ Object Mock based Solutions
▻ Apache Sling JCR Mock
▻ Magnolia Test Suite
▰ Jackrabbit based Solutions
▻ Jackrabbit
▻ In Memory JCR
212121
www.openweb.nl
Pros
▰ Make it much easier to setup test cases
▰ Make it very easy to switch between different
implementation of JCR Mock due to uniform
interaction with your mocking framework
▰ Tested with Magnolia tests, Apache Sling and In
Memory JCR. It should be able to work with JCR
Mock Up. Although it has not been tested.
▰ Licensed Under Apache License 2.0
222222
www.openweb.nl
Cons
▰ Lack of Documentation. Unfortunately there
isn’t any documentation available for JCR
Mocking tool at this point. Any contribution is
more than welcome.
232323
www.openweb.nl
Usage
242424
www.openweb.nl
Usage (with Apache Sling)
252525
www.openweb.nl
Usage (with In Memory Jcr)
262626
www.openweb.nl
Overview
▰ Mock JCR Implementations
▻ In memory JCR
▰ JCR Mocking Tool
▰ Hippo Unit Tester
272727
www.openweb.nl
Hippo Unit Tester
www.openweb.nl
Goal
29
Hippo Unit Tester is a framework that tries to take
advantage of JCR mocking solutions and Hippo
Mock objects to setup a mock Hippo API. It tries to
be as realistic as possible while still be highly
manipulatable. In order to enable developers to
focus on designing their unit tests scenarios
instead of focusing on technical details mocking
process.
2929
www.openweb.nl
Usage
303030
www.openweb.nl
Usage
313131
www.openweb.nl
Demo
32
http://bit.ly/2jbHQf6
3232
www.openweb.nl
Links
▰ In Memory JCR: https://github.com/openweb-nl/in-
memory-jcr
▰ JCR Mocking tool: https://github.com/openweb-nl/jcr-
mocking-tool
▰ Hippo Unit Tester: https://github.com/openweb-
nl/hippo-unit-tester
▰ An Example of Hippo Unit Tester: http://bit.ly/2jbHQf6
333333

Unit testing hippo

Editor's Notes

  • #11 3 months ago I did a similar presentation at Hippo Meetup NL (also known as Hippo.Campus) there I went into more detail about each one these frameworks here
  • #22 3 months ago I did a similar presentation at Hippo Meetup NL (also known as Hippo.Campus) there I went into more detail about each one these frameworks here