Unable to return ResponseEntity on WebClient.ResponseSpec.toEntity mock

Viewed 208

Test Class

@SpringBootTest
class TestClass {

@Mock
private WebClient.ResponseSpec responseMock;


@Test
public void testSampleMethod() {

//responseStr is valid json string
ResponseEntity<String> responseEntity = new ResponseEntity<String>(responseStr, HttpStatus.OK))

when(responseMock.toEntity(String.class)).thenReturn(responseEntity);

}

thenReturn() gives a compliation error - "Cannot resolve method 'thenReturn(org.springframework.http.ResponseEntity<java.lang.String>)'"

Is mocking ResponseEntity not allowed in Mockito? I can see an example of same here but it doesn't seem be to be accepted in my case. Is there any alternative way of mocking ResponseEntity.

1 Answers

The toEntity methods return Mono<ResponseEntity<T>>, so you should return Mono<ResponseEntity<String>>

when(responseMock.toEntity(String.class)).thenReturn(Mono.just(responseEntity));
Related