1

I'm trying to mock a RestTemplate exchange() call with the following code:

Test method

given(restTemplate.exchange(any(UriComponents.class), any(HttpMethod.class), any(HttpEntity.class), any(StatusResponse.class)))
            .willReturn(new ResponseEntity<>(HttpStatus.BAD_GATEWAY));

The code does not compile because:

  1. it complains that cannot resolve method willReturn(new ResponseEntity<>(HttpStatus.BAD_GATEWAY))
  2. it complains that cannot resolve method exchange(T, T, T, T)

How should I change the signature to make it work? Thanks.

esseara
  • 834
  • 5
  • 27
  • 47

2 Answers2

1

Take a look at docs at the exchange methods. I don't see any method which uses UriComponents in arguments.

As the first argument of exchange method you need to use String , URI or RequestEntity

Kamil W
  • 2,230
  • 2
  • 21
  • 43
  • Yes, my mistake. I was actually using `builder.toUri()` that returns a `String`. I wrongly used the type of the `builder` variable. – esseara Mar 26 '19 at 14:19
1

The 1st argument of exchange (url) should be eq("url") or anyString() (assumning that "url" is the value you're using in your test).

The 4th argument (response class) should be eq(StatusResponse.class) or any(Class.class).

amseager
  • 5,795
  • 4
  • 24
  • 47