title: unit testing of controller class which has rest template
body: <p>I have tried test on controller class, It is not mock the rest template which is in controller class.</p>
  <pre><code>@RestController
  public class LoginController {
  
      @RequestMapping(value = &quot;api/v1/login&quot;, method = RequestMethod.POST)
      public String Loginpostmethod(@RequestBody LoginUser login) {
  
          String url = &quot;https://jira2.domain.com/rest/auth/1/session&quot;;
          String username = login.getUsername();
          String password = login.getPassword();
          String authStr = username + &quot;:&quot; + password;
          String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes());
          HashMap&lt;String, String&gt; map = new HashMap&lt;&gt;();
          map.put(&quot;username&quot;, username);
          map.put(&quot;password&quot;, password);
          HttpHeaders headers = new HttpHeaders();
          headers.add(&quot;Authorization&quot;, &quot;Basic &quot; + base64Creds);
          headers.set(&quot;Content-Type&quot;, &quot;application/json&quot;);
          headers.set(&quot;Accept&quot;, &quot;application/json&quot;);
          RestTemplate restTemplate = new RestTemplate();
          HttpEntity&lt;HashMap&lt;String, String&gt;&gt; entity = new HttpEntity&lt;&gt;(map, headers);
          
          ResponseEntity&lt;String&gt; response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
          return response.getBody();
          
      }    
  }
  </code></pre>
  <p>controller test class</p>
  <pre><code>public class LoginControllerTest {
      
      @Mock
      RestTemplate mockrestTemplate;
      private String testUrl = &quot;https://jira2.domain.com/rest/auth/1/session&quot;;
      
      @MockBean
      LoginUser mocklogin;
      
      @InjectMocks
      LoginController loginController;
      
      @Test
      public void TestPostMethodSuccess() throws  Exception{
          LoginUser mocklogin=new LoginUser();
          mocklogin.setUsername(&quot;testusername&quot;);
          mocklogin.setPassword(&quot;testpassword&quot;);
          
          
          HashMap&lt;String, String&gt; map = new HashMap&lt;&gt;();  
          map.put(&quot;username&quot;, &quot;username&quot;);
          map.put(&quot;password&quot;, &quot;password&quot;);
          HttpHeaders headers = new HttpHeaders();
          headers.add(&quot;Authorization&quot;, &quot;Basic&quot;);
          headers.set(&quot;Content-Type&quot;, &quot;application/json&quot;);
          headers.set(&quot;Accept&quot;, &quot;application/json&quot;);
          
          HttpEntity&lt;?&gt; request = new HttpEntity&lt;&gt;(map, headers);
  
          ResponseEntity&lt;String&gt; response = new ResponseEntity&lt;&gt;(&quot;respons&quot;,HttpStatus.OK);
          
          
          Mockito.when(mockrestTemplate.exchange(testUrl, HttpMethod.POST, request, String.class)).thenReturn(response);
     
          assertEquals(200,response.getStatusCodeValue());
          loginController.Loginpostmethod(mocklogin);
      
      }
  </code></pre>
  <p>This what I have done is this is the proper way to test the controller class, It should call the api inside the controller class have a rest template so I have mocked the rest template which has the null value and assert function also is not working.When I use the verify syntax for when and then,the error as wanted but not invoked.</p>
  <hr />
  <p>Using mock Mvc,</p>
  <pre><code> @RunWith(SpringJUnit4ClassRunner.class)
  @AutoConfigureMockMvc
  @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
  @ActiveProfiles(&quot;test&quot;)
  public class LoginControllerTest {
      
      @Autowired
      private MockMvc mockMvc;
      
      @Mock
      LoginUser mocklogin;
      
      @InjectMocks
      LoginController loginController;
      
      @Test
      public void TestPostMethodSuccess() throws  Exception{
          LoginUser mocklogin=new LoginUser();
          mocklogin.setUsername(&quot;testusername&quot;);
          mocklogin.setPassword(&quot;testpassword&quot;);
          
          this.mockMvc.perform(MockMvcRequestBuilders
                  .post(&quot;/api/v1/login&quot;)
                  .contentType(MediaType.APPLICATION_JSON)
                  .content(asJsonString(mocklogin))
                  .accept(MediaType.APPLICATION_JSON))
                  .andDo(print())
                  .andExpect(MockMvcResultMatchers.status().isOk());
              
          }
      public static String asJsonString(final Object obj) {
          try {
              final ObjectMapper mapper = new ObjectMapper();
              final String jsonContent = mapper.writeValueAsString(obj);
              System.out.println(jsonContent);
              return jsonContent;
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }
  
      
  }
  </code></pre>
  <p>facing the error,</p>
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for &quot;https://jira2.domain.com/rest/auth/1/session&quot;: jira2.domain.com; nested exception is java.net.UnknownHostException: jira2.domain.com
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
      at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:72)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
      at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
      at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
      at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
      at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
  </code></pre>