简体   繁体   English

在 android 工作室中创建一个新的 JSONObject 返回一个 null object

[英]Creating a New JSONObject in android studio returns a null object

I am working on a Mockito project where I want to mock a volley class that sends and returns JSON objects.我正在研究一个 Mockito 项目,我想在其中模拟一个发送和返回 JSON 对象的齐射 class。 I wrote a simple test method just to make sure I was creating my JSON objects correctly and to my dismay, I found that when I use the constructor in this way JSONObject testObject = new JSONObject();我写了一个简单的测试方法只是为了确保我正确地创建了我的 JSON 对象,但令我沮丧的是,我发现当我以这种方式使用构造函数时JSONObject testObject = new JSONObject(); and then use testObject.put("Boolean", true) and then print out the boolean value of that testObject, the program returns false, instead of true.然后使用testObject.put("Boolean", true)然后打印出该 testObject 的 boolean 值,程序返回 false,而不是 true。

     @Test
public void testMethod() throws JSONException{
    JSONObject testObject = new JSONObject();
    testObject.put("Boolean", true);
    System.out.println(testObject.getBoolean("Boolean"));
}

    

Here are the import statements I am using for this class这是我用于此 class 的导入语句

    import org.json.JSONException;
    import org.json.JSONObject;
    import org.junit.Rule;
    import org.junit.Test;
    import org.mockito.junit.MockitoJUnit;
    import org.mockito.junit.MockitoRule;

Any ideas of what is going on?关于发生了什么的任何想法?

I tested the code you sent and the value printed true to me.我测试了您发送的代码,并且打印的值对我来说是真实的。 The only difference I can think of is trying我能想到的唯一区别是尝试

import org.junit.jupiter.api.Test;

instead of代替

import org.junit.Test;

To see if something changes看看有没有变化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM