【问题标题】:java.lang.NoSuchMethodError on using org.json.JSONTokener for schema validation failing when running junit test casejava.lang.NoSuchMethodError 在运行 junit 测试用例时使用 org.json.JSONTokener 进行模式验证失败
【发布时间】:2019-06-25 02:21:59
【问题描述】:

我添加了代码new JSONObject(new JSONTokener(EsimActivationProcessor.class.getResourceAsStream)) 以根据 json 模式验证输入 json 请求。服务器上的验证和正常运行。在使用 junit 测试同一类时,我收到 java.lang.NoSuchMethodError: org.json.JSONTokener.<init>(Ljava/io/InputStream;)V 错误。

我添加了以下jar:

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

我已尝试删除 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>

【问题讨论】:

    标签: java spring-boot junit


    【解决方案1】:

    编辑 1:

    我使用了 github-fge json-schema-validator,它在应用程序运行和测试用例中都有效。 依赖:

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

    代码sn-p:

    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();
    

    【讨论】:

    • 我的测试在运行时失败。添加此依赖项后,它解决了相同的错误
    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 2013-05-20
    • 1970-01-01
    • 2021-06-10
    • 2011-03-29
    相关资源
    最近更新 更多