Dax Durax
Dax Durax

Reputation: 1657

Mockito mocking restTemplate.postForEntity

I am trying to mock restTemplate.postForEntity method,

The actual method call is:

URI myUri = new URI(myString);
HttpEntity<String> myEntity ...


String myResponse = restTemplate.postForEntity(myUri, myEntity, String.class);

What I have in my test class is:

Mockito.when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), eq(String.class))).thenReturn(response);

This does not work; I have tried several other permutations with no success either. Any suggestions appreciated, thanks.

By this does not work I mean that the actual method is called and not the mocked method (so the desired result is not returned etc.)

Upvotes: 4

Views: 35167

Answers (6)

Justinas Jakavonis
Justinas Jakavonis

Reputation: 8818

You could test with MockRestServiceServer. Then it would return mock response, no matter what RestTemplate method or its overloading you use for POST call - postForEntity, postForObject or exchange.

For example:

@SpringBootTest
public class YourServiceTest {

    @Autowired
    private YourService yourService;

    @Autowired
    private RestTemplate restTemplate;

    private MockRestServiceServer mockServer;


    @BeforeEach
    public void setUp() {
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }
    
    @Test                                                                                          
    public void methodWithPostCallTest() {   
        
        mockServer.expect(ExpectedCount.once(), 
          requestTo(new URI("post-method-url")))
          .andExpect(method(HttpMethod.POST))
          .andRespond(withStatus(HttpStatus.OK)
          .contentType(MediaType.APPLICATION_JSON)
          .body("mock-response-body")
        );                                   
                       
        String response = yourService.methodWhichExecutesPost();

        mockServer.verify();

        assertEquals("mock-response-body", response);                                                        
    }
}

More details: Testing Client Applications

Upvotes: 0

anayagam
anayagam

Reputation: 121

This is what worked for me Firstly a resttemplate needs to be mocked in the test class

@Mock 
private RestTemplate mockRestTemplate;

Since ResponseEntity returns an Object create another method that returns the expected response wrapped in ResponseEntity

private ResponseEntity<Object> generateResponseEntityObject(String response, HttpStatus httpStatus){
    return new ResponseEntity<>(response, httpStatus);
}

In your test case, you can now mock the expected response as follows

String string = "result";
when(mockRestTemplate.postForEntity(anyString(), any(), any()))
        .thenReturn(generateResponseEntityObject(string, HttpStatus.OK));

Upvotes: 5

Abhishek Singh
Abhishek Singh

Reputation: 149

The following code works for me - when(mockRestTemplate.postForEntity(anyString(), any(), eq(String.class))).thenReturn(response);

Upvotes: 14

Pomagranite
Pomagranite

Reputation: 696

resttemplate returns ResponseEntity

Postforentity so something like ResponseEntitymyResponse= restTemplate.postForEntity(myUri, myEntity, StringToReturn.class); where StringToReturn is whatever type to return (just String in your case)

Upvotes: -1

Larry
Larry

Reputation: 348

I'd guess that the postForEntity method is final - you could use RestOperations instead of RestTemplate to work around that.

Upvotes: 0

Paulo Schreiner
Paulo Schreiner

Reputation: 1046

You have to make sure that you initialize the restTemplate as a mock in your tests

 RestTemplate restTemplate = mock(RestTemplate.class);

AND that this (mocked) rest template is the one being used in your actual method call. You could have a setRestTemplate() method on your object, and you could use that to set the restTemplate:

 myTestObject.setRestTemplate(restTemplate);

Mocks will never just call the original method, so if that's happening you can be pretty sure your actual method is not using a mock. (Real) mocks will either return what you told them to, or null.

Upvotes: -1

Related Questions