1

I'm working on spring boot application that makes three external api calls from service layer, so while writing the integration tests, I'm trying to override actual RestTemplate bean with mocked RestTemplate bean and when making the three external API calls I specified to return some data

given(this.restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(),
            ArgumentMatchers.isA(new ParameterizedTypeReference<TestData1>() {
            }.getClass()))).willReturn(ResponseEntity.accepted().body(new TestData1()));

    given(this.restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(),
            ArgumentMatchers.isA(new ParameterizedTypeReference<TestData2>() {
            }.getClass()))).willReturn(ResponseEntity.accepted().body(new TestData2()));

    given(this.restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(),
            ArgumentMatchers.isA(new ParameterizedTypeReference<List<TestData3>>() {
        }.getClass()))).willReturn(ResponseEntity.accepted().body(Arrays.asList(new TestData3())));

But the problem is when ever restTemplate.exchange method is called it is returning null as response instead of ResponseEntity with corresponding body

TestConfig class

@TestConfiguration
@ActiveProfiles("test")
@Profile("test")
public class TestConfig {

     @Bean
     public RestTemplate restTemplae() {
         return Mockito.mock(RestTemplate.class);
      }

TestClass

@SpringBootTest(classes = { TestConfig.class })
@ActiveProfiles("test")
@Profile("test")
@RunWith(SpringJUnit4ClassRunner.class)
public class TestClass {

 @Autowired
 private RestTemplate restTemplate;

  @Test
  public void taskTest() {

    //code

    given(this.restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(),
        ArgumentMatchers.isA(new ParameterizedTypeReference<TestData1>() {
        }.getClass()))).willReturn(ResponseEntity.accepted().body(new TestData1()));

given(this.restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(),
        ArgumentMatchers.isA(new ParameterizedTypeReference<TestData2>() {
        }.getClass()))).willReturn(ResponseEntity.accepted().body(new TestData2()));

given(this.restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(),
        ArgumentMatchers.isA(new ParameterizedTypeReference<List<TestData3>>() {
    }.getClass()))).willReturn(ResponseEntity.accepted().body(Arrays.asList(new TestData3())));

 //code
  }
app
  • 733
  • 5
  • 15
  • 27
  • 2
    `RestTemplate.exchange()` has *many* (8!) overloads! ..are you sure you're mocking the correct one? (please try to mock "more precise" than `any()`) – xerx593 Mar 05 '19 at 21:36
  • that's good point it will try specifying each type, but still this should work right? – app Mar 05 '19 at 21:38
  • yea... it is "known working" (mocking resttemplate) – xerx593 Mar 05 '19 at 21:39
  • ..and (maybe) you have to watch out for "varargs" (https://stackoverflow.com/q/2631596/592355). – xerx593 Mar 05 '19 at 21:43
  • No luck I tried specifying each type@xerx593 – app Mar 05 '19 at 21:49
  • ..then see, my latter comment: varagrs! ;), try: `given(/*as before, but one additional:*/ , ArgumentMatchers.any())` – xerx593 Mar 05 '19 at 21:51
  • ..i suppose you are trying to mock: [this one](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#exchange-java.lang.String-org.springframework.http.HttpMethod-org.springframework.http.HttpEntity-org.springframework.core.ParameterizedTypeReference-java.lang.Object...-) – xerx593 Mar 05 '19 at 21:52
  • this one ```public ResponseEntity exchange(java.net.URI url, HttpMethod method, @Nullable HttpEntity> requestEntity, ParameterizedTypeReference responseType) throws RestClientException``` – app Mar 05 '19 at 21:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189485/discussion-between-xerx593-and-app). – xerx593 Mar 05 '19 at 21:55

0 Answers0