Search code examples
javaspring-bootjunit

java.lang.NoSuchMethodError on using org.json.JSONTokener for schema validation failing when running junit test case


I have added the code new JSONObject(new JSONTokener(EsimActivationProcessor.class.getResourceAsStream)) to validate the input json request against the json schema. The validation on server and normal running. On testing the same class using junit I'm getting java.lang.NoSuchMethodError: org.json.JSONTokener.<init>(Ljava/io/InputStream;)V error.

I have added the following jar:

<dependency>
    <groupId>org.everit.json</groupId>
    <artifactId>org.everit.json.schema</artifactId>
    <version>1.3.0</version>
</dependency>

I have tried removing conflicting methods in the jar springboot-test jar:

<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>

Solution

  • EDIT 1:

    I have used github-fge json-schema-validator which is working in both application run as well as using the test case as well. The dependency:

    <dependency>
        <groupId>com.github.fge</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>2.2.6</version>
    </dependency>
    

    Code snippet:

    JsonNode node = JsonLoader.fromString(builder.toString());
    JsonNode data = JsonLoader.fromString(serviceCreateJson.trim());
    JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonSchema sch = factory.getJsonSchema(node);
    ProcessingReport report = sch.validate(data);
    boolean isSuccess = report.isSuccess();