Spring Boot, JUnit and org.json collision

org.json:json package provides a neat set of fuctions, including XML to JSON conversion.

While you can use the library in production code of a project based on Spring Boot.

It works fine in Spring Boot based production code, but when it comes to the tests you should expect to see java.lang.NoSuchMethodError: org.json.JSONObject.stringToValue(Ljava/lang/String;)Ljava/lang/Object;

Sping Boot starting from 1.5.4 detects this collision and shows the following message on startup:

Found multiple occurrences of org.json.JSONObject on the class path:

jar:file:/home/ilya/.m2/repository/com/vaadin/external/google/android-json/0.0.20131108.vaadin1/android-json-0.0.20131108.vaadin1.jar!/org/json/JSONObject.class
jar:file:/home/ilya/.m2/repository/org/json/json/20170516/json-20170516.jar!/org/json/JSONObject.class

You may wish to exclude one of them to ensure predictable runtime behaviour.

As you can see another library comes to play when we have dependency on spring-boot-starter-test. This one was initialy developed for Android. Dated 2013 year!

Little investigation shows that the cause is the licence of org.json, that contains statement  "The Software shall be used for Good, not Evil." Spring Boot maintainers are not going to include the same in their licence, that is why they have to use the alternative.

Digging lower we get to the root of the not evil: http://www.json.org/license.html

I am not a lawyer but this does not mean any JSON lib should include the statement. And many, including com.vaadin.external.google:android-json, do not.

The situation is insane and kills the spirit of open source, but as long you do not follow the path of evil, you can simply disable com.vaadin.external.google:android-json pulled by spring-boot-starter-test and keep dependency on org.json:json:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20170516</version>
</dependency>

Alternative is to use the org.json implementation bundled with Spring Boot but expect to see lack of the features.

Комментарии

Популярные сообщения из этого блога

Changing channel colors at Saleae Logic

Mime type issue with Geoserver and Spring