没有找到合适的方法用于 thenReturn。

huangapple go评论67阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月4日 03:59:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定