Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Fixed
-
Testing Sling Mock 2.3.4
-
None
Description
Whenever sling-mock is used where johnzon-core is managed from 1.0.0 to 1.1.0 the following exception is thrown when executing tests which leverage SlingContext.load().json(...)
[ERROR] testXYZ(my.package) Time elapsed: 0.018 s <<< ERROR! java.lang.NoClassDefFoundError: Could not initialize class org.apache.johnzon.core.JsonProviderImpl at sun.reflect.GeneratedConstructorAccessor33.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at javax.json.spi.JsonProvider.doLoadProvider(JsonProvider.java:103) at javax.json.spi.JsonProvider.provider(JsonProvider.java:64) at javax.json.Json.createReaderFactory(Json.java:76) at org.apache.sling.jcr.contentparser.impl.JsonContentParser.<init>(JsonContentParser.java:74) at org.apache.sling.jcr.contentparser.ContentParserFactory.create(ContentParserFactory.java:54) at org.apache.sling.testing.mock.sling.loader.ContentLoader.<init>(ContentLoader.java:119) at org.apache.sling.testing.mock.sling.context.SlingContextImpl.load(SlingContextImpl.java:336) at org.apache.sling.testing.mock.sling.context.SlingContextImpl.load(SlingContextImpl.java:326) at ...
The reason is that Johnzon 1.1 implements JSON-P 1.1 and therefore references interfaces from the newer spec (e.g. in https://github.com/apache/johnzon/blob/b2d205738c65fb53e768fca445fa611515417c19/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java#L38), which are not contained in JSON-P 1.0. Unfortunately the version of JSON-P cannot be overwritten by DependencyManagement as the GAV differs between JSON-P 1.0 (org.apache.geronimo.specs:geronimo-json_1.0_spec) and JSON-P 1.1 (org.apache.geronimo.specs:geronimo-json_1.1_spec).
An easy fix for this is to load the dependencies for both JSON-P 1.1 instead from within sling-mocks, that way it is compatible with both Johnzon-Core 1.0 and 1.1. (JSON-P 1.1 should be backwards-compatible, so works with both Johnzon-Core 1.0 and 1.1).