During this presentation we are going to look at multiple solutions that are available for mocking JCR and go through their pros and cons. Then we are going to have look at Hippo Unit Test to see how Hippo Unit tester leverages JCR mocking to enable us to write easy to read and maintain unit tests.
5. 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
6. 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
7. 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
10. 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
13. 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
14. 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
15. 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
20. 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
21. 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
22. 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
23. 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
29. 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
33. 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
Editor's Notes
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
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