0

I am using RestTemplate postForEntity method to post to an endpoint. If POST is successful then it returns status code of 201. I need help to write test case for this method using Mockito. Any suggestions are appreciated. Thank you

Here is my code

public int postJson(Set<String> last){
    try{

        LOGGER.info("Status code " + statusCode);
    }   catch (Exception e) {
        e.printStackTrace();
    }
    return statusCode;          
}
private HttpEntity getHttpEntity() {
    return new HttpEntity<>( null, getHttpHeaders() );
}

private HttpHeaders getHttpHeaders() {
    return headersBuilder.build();
}
user12707940
  • 131
  • 2
  • 5
  • 16
  • Your code is confusing. I see a `statusCode` of type `HttpStatus`, easily tested with `statusCode == HttpStatus.CREATED`. I see a dangling `}` followed by `return statusCode;` which seems to be for the method returning `int`, except that can't be, given that an `HttpStatus` is not an `int`. So, are you asking how to check if an `int` value is `201`, or how to check is an `HttpStatus` value is equal to `HttpStatus.CREATED`? – Andreas Jan 23 '20 at 05:17
  • @Andreas Edited my code. All I am looking to test is did the ```POST``` happened successfully, which I think by checking int value of status code is possible. Any suggestions? – user12707940 Jan 23 '20 at 06:02
  • You still haven't fixed the code to even *compile*, let alone show the part of the code where you intend to test the returned status code. `return statusCode;` will not compile, because **`statusCode` is undefined**. You do have a `HttpStatus statusCode` inside the `try` block, but it's not an `int` value, and it doesn't exist outside the `try` block where the `return` statement is. --- *What* are you trying to return? *Where* are you trying to test the status code? – Andreas Jan 23 '20 at 08:28
  • @Andreas I have updated my question here https://stackoverflow.com/questions/59885702/java-resttemplate-postforentity-unit-test?noredirect=1#comment105900380_59885702 Thanks – user12707940 Jan 23 '20 at 20:37

2 Answers2

1

There is a problem in your approach, you're confusing integration testing with unit testing here. If you want just to do unit test, then you can mock RestTemplate using @Mock. But if you want to verify the integration with some remote service and you're okay to verify this from controller by using @MockMvc, refer this answer.

prakashb
  • 160
  • 1
  • 8
-1

You can mock the below code in your test cases , below is the sample code , you can use Mockito.any or Mockito.eq methods to mock the objects .

ResponseEntity<String> result = restTemplate.postForEntity(url, new HttpEntity<>( request, getHttpHeaders() ), String.class);

@RunWith(MockitoJUnitRunner.class)    
public class Test {
       @InjectMocks
    private TestController testController;

    @Mock
    private RestTemplate restTemplate;   
        public void testRest() {
        ResponseEntity<String> result = Mockito.mock(ResponseEntity.class);
        Mockito.when(restTemplate.postForEntity(Mockito.any(String.class), Mockito.eq(HttpEntity.class), Mockito.any(String.class))).thenReturn(result);

        }
        }
Pandit Biradar
  • 1,777
  • 3
  • 20
  • 35