Mokito单元测试NoSuchMethodError:org.json.JSONObject.keySet()?

时间:2019-10-10 06:47:32

标签: junit mockito

我正在尝试使用unit对下面的JSONobject进行mockito测试。

JSONObject obj =  new JSONObject(JsonString);
obj.put("newEntry","UserId")
Set<String> objKeySet = obj.keySet();

添加依赖项

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

错误

 java.lang.NoSuchMethodError: org.json.JSONObject.keySet()Ljava/util/Set;

我的问题是,为什么Mokito运行程序无法找到方法keySet(),但是放置方法运行良好。我该如何解决。

预先感谢

1 个答案:

答案 0 :(得分:0)

我可以通过将排除部分添加到pom.xml文件中来对其进行修复

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