title: Why is my JSONObject related unit test failing?
body: <p>I'm running my tests using <code>gradle testFlavorType</code></p>
  
  <pre><code>JSONObject jsonObject1 = new JSONObject();
  JSONObject jsonObject2 = new JSONObject();
  jsonObject1.put("test", "test");
  jsonObject2.put("test", "test");
  assertEquals(jsonObject1.get("test"), jsonObject2.get("test"));
  </code></pre>
  
  <p>The above test succeeds.</p>
  
  <pre><code>jsonObject = new SlackMessageRequest(channel, message).buildBody();
  String channelAssertion = jsonObject.getString(SlackMessageRequest.JSON_KEY_CHANNEL);
  String messageAssertion = jsonObject.getString(SlackMessageRequest.JSON_KEY_TEXT);
  assertEquals(channel, channelAssertion);
  assertEquals(message, messageAssertion);
  </code></pre>
  
  <p>But the above two requests fail. The stack trace says that channelAssertion and messageAssertion are null, but not sure why. My question is: <strong>Why are the above two asserts failing?</strong></p>
  
  <p>Below is the <code>SlackMessageRequest</code>.</p>
  
  <pre><code>public class SlackMessageRequest
          extends BaseRequest {
      // region Variables
  
      public static final String JSON_KEY_TEXT = "text";
      public static final String JSON_KEY_CHANNEL = "channel";
  
      private String mChannel;
      private String mMessage;
  
      // endregion
  
      // region Constructors
  
      public SlackMessageRequest(String channel, String message) {
          mChannel = channel;
          mMessage = message;
      }
  
      // endregion
  
      // region Methods
  
      @Override
      public MethodType getMethodType() {
          return MethodType.POST;
      }    
  
      @Override
      public JSONObject buildBody() throws JSONException {
          JSONObject body = new JSONObject();
          body.put(JSON_KEY_TEXT, getMessage());
          body.put(JSON_KEY_CHANNEL, getChannel());
          return body;
      }
  
      @Override
      public String getUrl() {
          return "http://localhost:1337";
      }
  
      public String getMessage() {
          return mMessage;
      }
  
      public String getChannel() {
          return mChannel;
      }
  
  // endregion
  }
  </code></pre>
  
  <p>Below is the stacktrace:</p>
  
  <pre><code>junit.framework.ComparisonFailure: expected:&lt;@tk&gt; but was:&lt;null&gt;
      at junit.framework.Assert.assertEquals(Assert.java:100)
      at junit.framework.Assert.assertEquals(Assert.java:107)
      at junit.framework.TestCase.assertEquals(TestCase.java:269)
      at com.example.app.http.request.SlackMessageRequestTest.testBuildBody(SlackMessageRequestTest.java:30)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at junit.framework.TestCase.runTest(TestCase.java:176)
      at junit.framework.TestCase.runBare(TestCase.java:141)
      at junit.framework.TestResult$1.protect(TestResult.java:122)
      at junit.framework.TestResult.runProtected(TestResult.java:142)
      at junit.framework.TestResult.run(TestResult.java:125)
      at junit.framework.TestCase.run(TestCase.java:129)
      at junit.framework.TestSuite.runTest(TestSuite.java:252)
      at junit.framework.TestSuite.run(TestSuite.java:247)
      at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
      at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
      at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
      at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
      at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
      at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
      at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
      at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p><strong><em>EDIT 5:55PM EST</em></strong></p>
  
  <p>I've figured out that I can log with <code>System.out.println("")</code> and then see the results by running <code>gradle testFlavorType --debug</code> and by trial and error I've discovered the following weird situation:</p>
  
  <pre><code>@Override
  public JSONObject buildBody() throws JSONException {
      System.out.println("buildBody mChannel = " + mChannel);
      System.out.println("buildBody mMessage = " + mMessage);
      JSONObject body = new JSONObject();
      body.put(JSON_KEY_TEXT, getMessage());
      body.put(JSON_KEY_CHANNEL, getChannel());
  
      if (body.length() != 0) {
          Iterator&lt;String&gt; keys = body.keys();
  
          if (keys.hasNext()) {
              do {
                  String key = keys.next();
                  System.out.println("keys: " + key);
              } while (keys.hasNext());
          }
      } else {
          System.out.println("There are no keys????");
      }
  
      return body;
  }
  </code></pre>
  
  <p>For some reason, "There are no keys????" is printing out?!?!?!?! Why?!</p>
  
  <p><strong><em>EDIT 6:20PM EST</em></strong></p>
  
  <p>I've figured out how to debug unit tests. According to the debugger, the assigned JSONObject is returning <code>"null"</code>. I have no clue what this means (see below). Since I think this is relevant, my gradle file includes the following:</p>
  
  <pre><code>testOptions {
      unitTests.returnDefaultValues = true
  }
  </code></pre>
  
  <p>It's especially strange because if I construct a JSONObject inside the test, then everything works fine. But if it is part of the original application's code, then it doesn't work and does the above.</p>
  
  <p><a href="https://i.stack.imgur.com/V5edX.png" rel="noreferrer"><img src="https://i.stack.imgur.com/V5edX.png" alt="enter image description here"></a></p>