title: Java: 401 Unauthorized test case issue in Unit test case for spring boot, REST
body: <p>everyone.</p>
  
  <p>I have written a test case:</p>
  
  <p><strong>AppManagementApplicationTests.java</strong></p>
  
  <pre><code>@Test
  public void testWithAppNameAsNull() {
  
      Input input = new Input();
      // input type
      String inputTrue = input.appNameAsNullOfAppInfo();
      // response has been stored
      Response response = Response.status(401).entity("").build();
  
      HttpEntity&lt;String&gt; passingData = new HttpEntity&lt;String&gt;(inputTrue);
  
      ResponseEntity&lt;String&gt; result = this.restTemplate.postForEntity("/App", passingData, String.class);
      assertEquals(response.getStatus(), result.getStatusCodeValue());
  
  }
  </code></pre>
  
  <p><strong>Input.java</strong></p>
  
  <pre><code>public String appNameAsNullOfAppInfo() {
      String appName = null;
      String appVersion = "1.1";
      String appKey = "testkey";
      long timestamp = 1487076718;
  
      AppInfo obj = new AppInfo(appName, appVersion, appKey, timestamp);
  
      Gson gson = new Gson();
  
      // searialize into string and return
      return gson.toJson(obj);
  
  }
  </code></pre>
  
  <p>In my code I am sending</p>
  
  <blockquote>
    <p>return new ResponseEntity(gson.toJson(response), HttpStatus.UNAUTHORIZED);</p>
  </blockquote>
  
  <p>So when I run it using Postman it gives the status code as 401</p>
  
  <p><a href="https://i.stack.imgur.com/ywimQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ywimQ.png" alt="enter image description here"></a></p>
  
  <p>But while testing it is giving an error
  <a href="https://i.stack.imgur.com/P2bJU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P2bJU.png" alt="enter image description here"></a></p>
  
  <p>The URL that I am using is <strong><a href="http://localhost:8091/App/" rel="nofollow noreferrer">http://localhost:8091/App/</a></strong> and in the error it is showing <strong><a href="http://localhost:41636/App" rel="nofollow noreferrer">http://localhost:41636/App</a></strong>.</p>
  
  <p>And if instead of 401 UNAUTHORISED I change it to 400 BAD_REQUEST, the same test case works absolutely fine.
  I am not able to figure out why. The line number 139 at which it is showing the error is <strong>"ResponseEntity result ="</strong></p>
  
  <p>Thanks in advance.</p>