英文:
No suitable method found for thenReturn
问题
KinesisAsyncClient kinesisClient = mock(KinesisAsyncClient.class);
PutRecordResponse result = PutRecordResponse.builder().shardId("shard-12345").build();
when(kinesisClient.putRecord(any()))
.thenReturn(CompletableFuture.completedFuture(result));
英文:
I am new to java and mockito, trying to mock my
//kinesisClient is of type KinesisAsyncClient
PutRecordResponse result = kinesisClient.putRecord(putRecordRequest).get();
like below:
KinesisAsyncClient kinesisClient = mock(KinesisAsyncClient.class);
PutRecordResult res = mock(PutRecordResult.class);
res.setShardId("shard-12345");
when(kinesisClient.putRecord(any()).get())
.thenReturn(res);
it's giving
error: no suitable method found for thenReturn(PutRecordResult)
.thenReturn(res);
I tried
KinesisAsyncClient kinesisClient = mock(KinesisAsyncClient.class);
final PutRecordResult res = new PutRecordResult();
res.setShardId("shard-12345");
when(kinesisClient.putRecord(any()).get())
.thenReturn(res);
but same error. I am not sure what I am doing wrong here. Can anyone help me on this?
答案1
得分: 1
The reason for this error is that the thenReturn method expects a PutRecordResponse object, but you are passing in a PutRecordResult object.
To fix this error, you should create a PutRecordResponse object and set its sequenceNumber and shardId fields to appropriate values, then use it in the thenReturn method like this:
KinesisAsyncClient kinesisClient = mock(KinesisAsyncClient.class);
PutRecordResponse res = mock(PutRecordResponse.class);
when(res.getSequenceNumber()).thenReturn("12345");
when(res.getShardId()).thenReturn("shard-12345");
when(kinesisClient.putRecord(any())).thenReturn(res);
// Call the method that uses the mocked KinesisAsyncClient
Here, we are mocking the PutRecordResponse object and setting its sequenceNumber and shardId fields to appropriate values. Then, we are using the thenReturn method to return a CompletableFuture that completes with the mocked PutRecordResponse object when the putRecord method is called on the mocked KinesisAsyncClient.
英文:
The reason for this error is that the thenReturn method expects a PutRecordResponse object, but you are passing in a PutRecordResult object.
To fix this error, you should create a PutRecordResponse object and set its sequenceNumber and shardId fields to appropriate values, then use it in the thenReturn method like this:
KinesisAsyncClient kinesisClient = mock(KinesisAsyncClient.class);
PutRecordResponse res = mock(PutRecordResponse.class);
when(res.getSequenceNumber()).thenReturn("12345");
when(res.getShardId()).thenReturn("shard-12345");
when(kinesisClient.putRecord(any())).thenReturn([PutRecordResponse type]);
// Call the method that uses the mocked KinesisAsyncClient
Here, we are mocking the PutRecordResponse object and setting its sequenceNumber and shardId fields to appropriate values. Then, we are using the thenReturn method to return a CompletableFuture that completes with the mocked PutRecordResponse object when the putRecord method is called on the mocked KinesisAsyncClient.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论