title: Using JSONObject#toString in Unit Tests (Android Studio)
body: <p>I would like to check if one of my classes deals fine with a JSON object. For that I decided to provide it with a JSONObject using the unit test environment. But it consistently failed to provide the desired results... until I tested the behavior of the <strong>JSONObject in the test method</strong> itself. </p>
  
  <p>This is the code:</p>
  
  <pre class="lang-java prettyprint-override"><code>    @Test
      public void checkJsonObject() {
  
          JSONObject obj;
          try {
  
              obj = new JSONObject();
              obj.put("front", "cat");
              obj.put("back", "chat");
              obj.put("bucket", 2);
  
              System.out.println("Object is " + (obj == null ? "" : "not") + " null");
              System.out.println("JSON content is: " + obj.toString());
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>These are the obtained results:</p>
  
  <pre><code>Object is not null
  JSON content is: null
  </code></pre>
  
  <p>Whereas the <strong>expected</strong> result is:</p>
  
  <pre><code>Object is not null
  JSON content is: {  "front"     : "cat",
                      "back"      : "chat",
                      "bucket"    : 2
                  }
  </code></pre>
  
  <p>(or similar).</p>
  
  <p>So, the JSONObject is not null. But the .toString() method returns null.</p>
  
  <p>What am I missing in the code?</p>
  
  <p>In case, it matters, the dependencies of the app contain:</p>
  
  <pre><code>testImplementation 'junit:junit:4.12'
  testImplementation 'androidx.test:core:1.0.0'
  testImplementation 'org.mockito:mockito-core:1.10.19'
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Thank you, guys, in the comments. <code>testImplementation "org.json:json:20190722"</code> did work. Maybe someone could provide an answer how to make it work for all cases of this type, so I don't have to look for what's wrong for half a day... Running tests on the phone? Inclusion of a particular android package? How is that done?... Or maybe, at least, do something, so Android Studio provides a warning that some (seemingly) standard class is not implemented in the tests. </p>